• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

WordPress Tips, Code Snippets, and How To Tutorials

  • Home
  • Blog
  • Tips
  • Snippets
  • Tutorials
  • Contact

How to create a shortcode to display the WordPress site url

September 1, 2015 by admin 6 Comments

Shortcodes are a very useful tool when using WordPress. There are often things you may do a lot and using a shortcode helps make it so you can use one instead of retyping a lot of content. Also, if you are a web developer, you may be creating WordPress themes/sites on a development server that will later be transferred to a client’s server. When doing this, the url during development may not be the same as will be after it is transferred over to the client’s live site and server. This can cause extra work after making the transfer editing links and other text that may have displayed the url. You can use a shortcode to find the current site url and display it based on the url the WordPress theme is currently installed on, so no matter where the theme is used, it will display the correct url, which means you won’t have to change it if you move the theme to a new url. Just add this WordPress Code Snippet to the functions.php file in your current WordPress theme in order to create a shortcode to display the WordPress site’s current url.

/**
 * Get site url for links 
 *
 * @author WPSnacks.com
 * @link https://www.wpsnacks.com
 */
function url_shortcode() {
return get_bloginfo('url');
}
add_shortcode('url','url_shortcode');

That’s it, now you can just type [url] anywhere you want the url to display throughout the site You can use this shortcode inside a link or other code as well(Can be used for the “Home” link in a navigation bar for example). Hope this helps, enjoy!

If you like this then you should look up more of our WordPress Tips Code Snippets and Tutorials.

Disclosure: There are affiliate links on posts and throughout the website and if you use the links to make a purchase we may earn a commission from it. We link to these companies and their products because of the quality of the companies and not because of the commission we may receive from it.

Disclaimer: The information contained in this website is for general information purposes only and in no event will this website or its owners be liable for any losses or damages associated with your use of our website or content. We recommend that you make a backup of your website before you make updates to it. Click here to see our full Disclaimer.

Filed Under: Code Snippets Tagged With: Functions.php, Links, Shortcodes

Reader Interactions

Comments

  1. phall says

    June 24, 2013 at 10:47 pm

    Thanks for your example of building a shortcode.  It provide a good template for build one for displaying permalinks of posts. by using get_permalink()

    Reply
    • phall says

      June 24, 2013 at 10:50 pm

      line 11 has an extra “}” that is not needed.

      Reply
      • WPSnacks says

        June 24, 2013 at 11:52 pm

        @phall Thank you for catching that, it was a typo on my part and I have now fixed it.  I am glad this code helped you and thanks again for the feedback.

        Reply
  2. webmasterintexas says

    February 24, 2014 at 2:50 pm

    This is not working for me. It is still spitting out [url] in firebug.

    Reply
  3. Eric says

    May 29, 2017 at 2:11 am

    Worked like a charm. Thanks for posting!

    Reply
  4. Arun Verma says

    September 26, 2017 at 7:42 am

    Yes, it works for me.
    Thanks

    Reply

Leave a Reply Cancel reply

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

Primary Sidebar

Hey, We Deliver... Anywhere!

Enter Email Address below to receive our snacks via email:

Join Over 1200 Readers!

Jump To A Random WordPress Tip

Copyright © 2019 • wpsnacks.com