How to Create a Shortcode in WordPress

by | Feb 21, 2016 | Web Development

Need Help Marketing Your Business Online?

Schedule a call to learn how we can help

Shortcodes are extremely helpful when developing a theme in WordPress. When developing a theme, you sometimes run into situations where you need custom functionality for design purposes or a specialized element. But for security reasons, WordPress strips all of the php code from pages and posts.

WordPress may think that it’s doing you a favor, but sometimes it makes it a nightmare to add a php snippet. This is where shortcode can save you a lot of time and headaches.

Shortcodes are very useful tools. For example, if you develop a WordPress theme/site on your local machine that will later be transferred to a live/client-server, you understand the pain of having to change all the links from the page links, images, files, etc. And after you’ve changed all the links you still have to check them again with a tool like Integrity (mac) to be 100% sure all the links for the new site goes to the right domain. This is where shortcodes come in handy.

With a shortcode, you can grab the site’s URL and add it to your pages or posts. This will make it easier to transfer your WordPress site to a new domain.

function url_shortcode() {
	return get_bloginfo('url');
}
add_shortcode('_url','url_shortcode');

That’s all you have to do. Now you can add this code snippet in your functions.php and use it by just typing [_url]. Remember, if you add multiple code snippets, it is better to make them their own plugin.