how to create shortcode using plugin

02:40
shortcode is a small snippet code to display dynamic data in your pages or posts or widgets. wordpress allows to create new short code by using plugin or funcitons.php wordpress comes with several builtin shortcodes. they are
  • audio
  • video
  • caption
  • .. etc

Hello World Shorcode

when ever place [helloworld] in your posts or pages or widgets the `helloworldfunc' function is executed. please create following plugin.php file in your plugin 'wp-content/plugins/' directory.
<?php
/*
Plugin name: New ShortCode
Description: A simple plugin to create short code.
Author: Sekhar
Version: 0.5
*/
add_shortcode('helloworld','helloworldfunc');
function helloworldfunc()
{
 return '<h2>Hello World</h2>';
}
?>

Shortcode with parameters

Also shortcode allows us to pass parameters and content to the shortcode function.
<?php
/*
Plugin name: New ShortCode
Description: A simple plugin to create short code.
Author: Sekhar
Version: 0.5
*/
add_shortcode('helloworld','helloworldfunc');
function helloworldfunc($params,$content = '')
{
 extract(
   shortcode_atts(
     array('style'=>''),
     $params
     )
  );
  
 return "<h2 style='".$style."'>".$content."</h2>";
}
?>

No comments:

Powered by Blogger.