Essential Tips for Using Shortcodes in WordPress

Shortcodes in WordPress allow you to add various functionality into posts, pages, and widgets without actually writing any code. Hence the name, Shortcode. Many WordPress plugins and some themes use shortcodes to add different elements such as pricing grid, event calendar, etc into WordPress.

What are Shortcodes in WordPress?

Any content added in a WordPress site goes through some security checks and filters. These security checks and filters make sure that no malicious code goes into posts, pages, comments, etc. This means you can’t directly write code in these areas.On the other hand, sometimes you just need to add code in these areas. Shortcodes provide a way to do that. Shortcodes make it easier to add other elements into WordPress posts.

Now then, let’s take a look at some tips for using shortcodes in WordPress.

Know When Not to Use Shortcodes

Shortcodes are great, but using shortcodes in every post is not a great idea.However if you use a shortcode in every post, then you are forever tied to the specific theme that’s providing the shortcode. If you’re using a theme specific shortcode to create call-to-action buttons inside your posts or pages, then you should look at using our guide on how to add CSS buttons in WordPress without using shortcodes.


If you’re using finding yourself adding the shortcode in every theme to add the same element such as a banner ad or signature text at the end of your post, then you may want to use a plugin or hire a developer to code that directly into your theme. This will make it easier to style that element and easily remove it should you decide to do that.


Future Proof Your Shortcodes

Shortcodes are great, but if it’s provied by your theme, then you may want to think twice about excessively using it. Because if you change your theme, then your next theme most likely will not have the same shortcode.


The best way to prevent that is adding your in a site-specific plugin.Simply copy and paste the shortcode snippet from your theme’s functions.php file, and then paste it in your site-specific plugin.


How to Search for Shortcode in Your WordPress Theme

In order to future proof your shortcode, you must know what the shortcode function looks like and how to find it in your theme. First you need to open your theme’s folder which is usually found in /wp-content/themes/your-theme-name/


You want to look inside the functions.php file or if the theme has an includes folder, then inside there. Open the files and search for the term add_shortcode. Here’s an example of what a shortcode snippet looks like:

function shortcode_fun() {
$i = '<p>This is my First Shortcode</p>';
return $i;
add_shortcode('my-shortcode', 'shortcode_fun');

This code creates a shortcode ‘my-shortcode’, which returns a simple text greeting and can be embedded into a WordPress post or page like this:


Using Shortcodes in Widgets

Often users think that shortcodes are limited to posts and pages, but they are not. You can use it inside your WordPress text widgets. Simply drag and drop a text widget to your sidebar and add your shortcode inside it.


Remember, this feature is not enabled by default in WordPress. In case you can’t see your shortcode in a widget, then you need to add this code in your theme’s functions.php file

add_filter('widget_text', 'do_shortcode');

Add Shortcode in Theme Files

If for some reason, you find a need to output the shortcode inside a non-widget area of your theme, then you can use your shortcodes there as well. You want to include a shortcode to display a contact form. Simply add your shortcode, like this:

<?php echo do_shortcode("[example_shortcode]"); ?>

Hiding a Broken Shortcode

Often users change their themes without realizing that their old shortcodes will not work. Sometimes, they find out after months when a user visits their old post to find odd text there. Well, you have two ways to fix it. You can either manually go and remove the shortcode from every post, or you can simply hide the broken shortcode.


All you need to do is add the following code in your theme’s functions.php file or a site-specific plugin.


add_shortcode( 'shortcodetag', '__return_false' );

Finding Shortcodes used in Posts

In case you don’t want to use the hack in Tip 6, and rather want to remove all shortcodes manually, then the first step is to find all posts that are using the shortcode. You can use this code in your theme’s functions.php file or a site-specific plugin to do the hard work for you.

function wp_shortcode($atts, $content=null) {
extract( shortcode_atts( array(
'find' => '',
), $atts ) );

$string = $atts['find'];

$args = array(
's' => $string,

$the_query = new WP_Query( $args );

if ( $the_query->have_posts() ) {
echo '<ul>';
while ( $the_query->have_posts() ) {
$the_query->the_post(); ?>
<li><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></li>
echo '</ul>';
} else {
echo "Sorry no posts found";

return ob_get_clean();
add_shortcode('shortcodefinder', 'wp_shortcode');