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!
phall says
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 says
line 11 has an extra “}” that is not needed.
WPSnacks says
@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.
webmasterintexas says
This is not working for me. It is still spitting out [url] in firebug.
Eric says
Worked like a charm. Thanks for posting!
Arun Verma says
Yes, it works for me.
Thanks