← back to code snippets


Creating a WordPress shortcode

Posted on March 4th, 2014 in WordPress by Graeme

    function url_shortcode()
    {
      return get_bloginfo('url');
    }
    add_shortcode('url','url_shortcode');
This is pretty self explanatory if you’re familiar with WordPress - add the above code into functions.php of your theme (it’s the quickest way). The above example defines a function called url_shortcode(), which simply returns the URL of the WordPess site.
Then directly underneath the function, we add an action hook (or whatever they’re called): add_shortcode which tells wordpress to include our function as a shortcode…which we can then use in blog posts.
So in a blog posts, you can now just write [url] and that will be the link to your blog.