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
- Pair =>which is an array
- Parameter => same variable name passed in function
- 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 “”.
?>
Tried alot of website but here step by step guide is amazing