WordPress Tip: bloginfo as a shortcode

I often need to help move a client’s redesigned website into WordPress at a new webhost. During this process, the client’s domain must remain pointing to the old website so there is no interruption in traffic. Repointing the domain becomes one of the last steps of the process.

WordPress addresses a potential problem here with a Template tag called bloginfo. Inside my theme’s templates, I use bloginfo(‘url’) rather than hardcoding my site’s URL. The actual URL is maintained in the Dashboard under Settings > General > WordPress address (URL). bloginfo(‘template_url’) adds the current theme path to the URL. Often when programming, computing a value gives you more flexibility than hardcoding a value.

Another area where I would like to have this flexibility is inside the actual content of the post. If I upload an image using the Media Uploader, it immediately computes the URL for the image. But I don’t want to have to come back later after the domain has been repointed to edit the URLs in the post. Can’t I have the same delayed computation that bloginfo(‘url’) provides, but inside the content?

Solution 1: Shortcodes for your URLs

Edit your theme’s functions.php

function my_url($atts, $content = null) {
  return get_bloginfo('url'); 
add_shortcode("url", "my_url");  
function my_template_url($atts, $content = null) {
  return get_bloginfo('template_url'); 
add_shortcode("template_url", "my_template_url");  
function my_images_url($atts, $content = null) {
  return get_bloginfo('template_url') . '/images'; 
add_shortcode("images_url", "my_images_url");

Using this shortcode, I can upload the image, insert the image into the post, and then modify the URL to use the shortcode. I don’t have to return to edit the URL later.

  <img src="[images_url]/chunky.jpg" />

Solution 2: Shortcode for bloginfo itself

I found this solution at Blue Anvil

function bloginfo_shortcode( $atts ) {
        'key' => '',
    ), $atts));
    return get_bloginfo($key);
add_shortcode('bloginfo', 'bloginfo_shortcode');


That was uploaded with the Media Uploader, and here’s the final markup

<a href="[bloginfo key='url']/wp-content/uploads/2010/02/chunky-e1266723693872.jpg">
<img src="[bloginfo key='url']/wp-content/uploads/2010/02/chunky-e1266723693872-225x300.jpg" 
alt="" title="chunky" width="225" height="300" class="alignnone size-medium wp-image-462" /></a>

4 thoughts on “WordPress Tip: bloginfo as a shortcode

  1. Pingback: Things I Found Interesting Around May 15th | Chris Coyier

  2. Hey, very usefull information! I was looking for something like this but with different use. Let’s say I have this WP theme where I let the user create a form, to have the form working I need to have ofcourse simply setting this to form.php will not work but you need


    But with $url = get_bloginfo('template_url)' I could simply create this output.

    So thanks again, very usefull!

  3. Here’s another use-case for “bloginfo as a shortcode.”

    The content of the post doesn’t matter much to me, because when I’m ready to launch/migrate I just do a find/replace on the blog content.

    One thing that doesn’t work with the find/replace is the sidebar widgets. So if I have an image or link in a widget, I had to manually update those. But now I use this:

    /* Shortcodes in Widgets */
    add_filter(‘widget_text’, ‘do_shortcode’);
    add_shortcode(‘template-url’, ‘template_shortcode’);

    function url_shortcode($atts) {
    return get_bloginfo(‘url’);

    function template_shortcode($atts) {
    return get_bloginfo(‘template_url’);

    That turns on the shortcode functionality for widgets, and creates a shortcode for url and template-url.

Leave a Reply

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