How to create a shortcode to display the WordPress site url

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 http://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!

   Related Sponsored Links

Disclaimer: The information contained in this website is for general information purposes only and in no event will this site or its owners be liable for any losses or damages associated with your use of our site or content. Click here to see our full Disclaimer.
5 comments
webmasterintexas
webmasterintexas

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

Web Designing
Web Designing

I am really stunned at this outstanding things. I always want to study the quality material and this thing I discovered in you publish. Thanks for discussing.

phall
phall

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()

phall
phall

line 11 has an extra "}" that is not needed.

WPSnacks
WPSnacks moderator

 @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.