Monday, July 8, 2013

How to add Bloginfo Shortcode in WordPress

In this tutorial i will show you how to add bloginfo shortcode using visual editor or directly on template files.

Bloginfo() function in WordPress gives you access to lots of useful information about your site. See the complete list in codex. To access all these values from inside Page/Post content itself, we can make a shortcode to return the values.

Add this php code to your functions.php file of your active theme:
function digwp_bloginfo_shortcode( $atts ) {
extract(shortcode_atts(array(
'key' => '',
), $atts));
return get_bloginfo($key);
}
add_shortcode('bloginfo', 'digwp_bloginfo_shortcode');

Now you can output any of the values by calling that shortcode with "key" in Visual Editor. For example, the name of your site:

[bloginfo key='name']
Or directly to images in your theme folder: 

<img src="<?php do_shortcode('[bloginfo key='template_url']'); ?>/images/logo.jpg" alt="<?php do_shortcode('[bloginfo key='name']'); ?> logo" />
Disqus Comments