How to create a shortcode to display the WordPress theme path

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 theme path 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 image paths and other text that may have displayed the theme path. You can use a shortcode to find the current WordPress theme’s stylesheet directory and display that path, so no matter where the theme is used, it will display the correct path, which means you won’t have to change it if you move the theme to a new location. I use this often when developing themes for clients so that when I put an image inside the themes image folder during development, when the theme is later placed on the client’s server, the image path still works without me having to change it. 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 theme’s path.

/**
 * Get stylesheet directory for images
 *
 * @author WPSnacks.com
 * @link http://www.wpsnacks.com
 */
function child_shortcode() {
return get_bloginfo('stylesheet_directory');
}
add_shortcode('path', 'child_shortcode');
}

That’s it, now you can just type [path] anywhere you want the theme’s path to display throughout the site. You can use this shortcode inside an image source path or other code as well. 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.

Comments

  1. Nice article, thanks for the information.
     
    Anna @ http://griyamobilkita.webs.com

  2. Wow, Hey thanks for this codes. It will be needful to me in my future project.

Leave a Comment

*