Wednesday, 13 April 2016

How to create wordpress Shortcode?

How to create wordpress Shortcode?

Question: What is Shortcode in Wordpress?
A shortcode is code that lets you do nifty things with very little effort.


Question: What is use Shortcode in Wordpress?
Shortcodes can be embed in post Or page which can do lot of things like Image Gallery, Video listing etc.


Question: Give a simple Shortcode example?
[gallery]

It would add photo gallery of images attached to that post or page.


Question: What is Shortcode API in Wordpress?
Shortcode API is set of functions for creating shortcodes in Page and Post.


The Shortcode API also support attributes like below:
[gallery id="123" size="large"]



Question: What is name of function which is used to register a shortcode handler?
add_shortcode

It has two parameter.
first is shortcode name and second callback function. For Example:
add_shortcode( 'shortcode', 'shortcode_handler' );



Question: How many parameter can be passed in callback function?
  1. $atts - an associative array of attributes.
  2. $content - the enclosed content
  3. $tag - the shortcode tag, useful for shared callback functions



Question: How to create shortcode in wordpress? Give working example? Create Shortcode
function testdata_function() {
  return 'This is testing data. This is testing data. This is testing data. This is testing data. This is testing data. This is testing data.';
}
add_shortcode('testdata', 'testdata_function');

Use Shortcode
[testdata]



Question: How to create shortcode with attributes in wordpress? Give working example?
Create Shortcode
function testdata_function($atts) {
    extract(shortcode_atts(array(
      'header' => 'This is default heading',
      'footer' => 'This is default footer',
   ), $atts));

  return '<b>'.$header.'</b>
This is testing data. This is testing data. This is testing data. This is testing data.
<b>'.$footer.'</b>';
}
add_shortcode('testdata', 'testdata_function');

Use Shortcode
[testdata header="Custom Heading" footer="Custom footer"]



Question: How to create shortcode with in wordpress? Give working example?
Create Shortcode
function testdata_function($attr, $content) {
  return $content.' This is testing data. This is testing data. This is testing data. This is testing data. This is testing data. This is testing data.';
}
add_shortcode('testdata', 'testdata_function');


Use Shortcode
[testdata]This is content[/testdata]



Question: What to do if Ampersand (i.e &) converted to &#038;?
Use html_entity_decode function (PHP inbuilt function).


Question: How to use meta tags in shortcode?
If you want to add meta tags in head tag. then use below function.
add_action('wp_head', 'add_meta_tags');
function add_meta_tags() {
        echo '';
    }



Monday, 11 April 2016

YII interview questions and answers for fresher

yii interview questions and answers for fresher

Question: What is Yii?
Yii is a PHP framework which is based on MVC (Model View Controller).


Question: Is it opensource?
Yes, It is opensource. Download and use as per your project requirement. Question: What is full form of Yii
Yes it is.


Question: In which language, It is written?
PHP.


Question: What is current stable version of Yii?
Version: 2.0.7 dated February 14, 2016.


Question: What is offical website of Yii Framework?
http://www.yiiframework.com


Question: From where i an download Yii Framework?
http://www.yiiframework.com/download


Question: How to start Yii?
http://www.yiiframework.com/tour


Question: What are main feature of Yii framework?
  1. MVC design pattern
  2. Web Service available for Apps like android
  3. Internationalization and localization translation for multilingual.
  4. Caching for speed up the application
  5. Error handling and logging for tracking
  6. cross-site scripting (XSS), cross-site request forgery (CSRF) protection
  7. PHPUnit and Selenium for testing.
  8. Automatic code generation help us to fast development.



Question: How to set default controller on Yii?
array(
    'name'=>'Yii Framework',
    'defaultController'=>'site',
);



Question: How to get current controller id?
echo Yii::app()->controller->id;



Question: How to get current action id?
echo Yii::app()->action->id;



Question: What is the first function that gets loaded from a controller? ?
index



Question: How can we use ajax in Yii?
use ajax helper


Question: What are two type of models in YII
  1. Form models
  2. active records


Question: What are active records model?
Active Record is a design pattern used to abstract database access in an object-oriented way.
active records model is based on this design pattern.


Question: How to define a form model?
class MyLoginForm extends CFormModel
{
    public $username;
    public $password;
    public $rememberMe=false;
}



Question: How to set validation in Form Model?
class MyLoginForm extends CFormModel
{
    public $username;
    public $password;
    public $rememberMe=false; 
    private $_identity;
 
    public function rules()
    {
        return array(
            array('username, password', 'required'),
            array('rememberMe', 'boolean'),
            array('password', 'authenticate'),
        );
    }
 
    public function authenticate($attribute,$params)
    {
        $this->_identity=new UserIdentity($this->username,$this->password);
        if(!$this->_identity->authenticate())
            $this->addError('password','username or password Incorrect .');
    }
}



Question: What are the core application components available on Yii?
  1. db- database connection.
  2. assetManager - manage the publishing of private asset files
  3. authManager - manage role-based access control
  4. cache- manage caching functionality
  5. clientScript- manage javascript and CSS
  6. coreMessages- provides translated core messages
  7. errorHandler- manage errors handling.
  8. themeManager- Manage themes
  9. urlManager- URL parsing and creation functionality
  10. statePersister- mechanism for persisting global state
  11. session- Session management
  12. securityManager- Security Managment