Create Short code in WordPress using Action Hook

Create Short code in WordPress using Action Hook

<?php

/**

DOC LOG

*/

defined(‘ABSPATH’)||die(“You can’t access this file directly”);

add_action(‘init’,’shairy_init’);

function shairy_init()

{

            add_shortcode(‘test’,’shairy_my_shortcode’);

}

/*function shairy_my_shortcode($par)

{

            return “Hello Guys this is my First Plugin”;

}*/

Now Here test is the shortcode name and you can use this shortcode anywhere in the program. It will display the output Hello Guys this is my First Plugin.

If you want to add custom message at run time then shortcode_atts() is used and it has 3 paramater

  1. Pair =>which is an array
  2. Parameter => same variable name passed in function
  3. short code => name of our shortcode i.e. test here

function shairy_my_shortcode($par)

{

            $par=shortcode_atts(array(

            ‘message’=>’Hello Guys this is my First Plugin’,

            ),$par,’test’);

            return $par[‘message’];

}

Now Here [test message=””] is the shortcode name and you can use this shortcode anywhere in the program. It will display the output given in the message “”.

?>

One thought on “Create Short code in WordPress using Action Hook

Post Your Comments & Reviews

Your email address will not be published. Required fields are marked *

*

*