Best Jquery ,javascript, HTML5 CSS3 tutorial with jquery examples, php script , demos for beginners, web designers and web developers.

Jquery

Jquery, javascript Training

Jquery training blog

What is WordPress shortcode

WordPress shortcode nothing just it call a function written in theme’s function.php and returns the output. WordPrees introduced shortcode from WordPress 2.5 version.
Shortcode allows execute code inside WordPress page or post or widget without writing code in core function of wordpress. Alternatively shortcodes allows to add new feature to your wordpress site by means of shortcode where your code will play a role as per your requirement. Shortcode simplified the way of addition of new feature in your wordpress site

WordPress shortcode :

An example of custom shortcode to add a sitelogo to your site

[site_logo]

You can develop any type of custom shortcode as per your requirement.

Now to how to write a shortcode and how to display it.

In the example we are going to add custom sitelogo to our site  using shortcode.

Firstly open your theme folder in which you want to apply short code or add a new feature. if it has child them then go to the function.php of the chaild theme or if no child theme go directly to the theme folder you will find a function.php file. Open the function.php file in any text editor or editor you like.

There are the steps in WordPress shortcode

  1. Register shortcode
  2. Write function for the shortcode
  3. Use the shortcode

First register the WordPress shortcode by writing the following code

add_shortcode('site_logo', 'addSiteLogo');

Now write the function for the shortcode. As in example “addSiteLogo” will be the name of  our function or you may write as per your choice.

The below function ask for parameters. If parameter available it will take that value otherwise it will take the default value. in our example width and height is the parameter.

function addSiteLogo($params) {
extract(shortcode_atts(array(
'width' => 300,
'height' => 250,
), $params));
return '<img src="http://localhost/wpthem/wp-content/uploads/2017/05/sitelogo.jpg" height="'. $width . '" width="'. $height . '" />';
}

Now call the shortcode by adding in page or by doing echo in php file

To echo the shortcode

<?php echo do_shortcode('[site_logo width="1000" height="500"]');?> With parameter
<?php echo do_shortcode('[site_logo]');?> Without parameter

To add shortcode in page just paste the shortcode in page

[site_logo width="1000" height="500"]

 

Please follow and like us:





Got Something To Say:

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

*


About the author

Joydeb Choudhury holds a Master's degree in Computer Application from I.G.N.O.U. After working as a web developer in various companies he has started bloging in 2016.

Recent Posts of blog

Downalod Free Php Scripts