Currently set to No Index
Currently set to No Follow

Get the Official WPSaviour App Now!

Beginner’s GuideBlog

7 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. In this article, we will share 7 essential tips for using shortcodes in WordPress.

Using Shortcodes

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. For example, you can add a beautiful responsive slider using a simple shortcode. You can create a complex survey form in WordPress without writing a single line of code.

Having said that, lets take a look at some tips for using shortcodes in WordPress.

Tip 1. Know When Not to Use Shortcodes

Shortcodes are great, but using shortcodes in every post is not a great idea. There are many WordPress themes out there that proudly claim to have 200+ shortcodes.

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.

Remember, if you use a shortcode in every post and later want to remove it, then you will have to edit all the posts to manually remove it. Although there is an easier way that we will show you later in this article.

Tip 2: Future Proof Your Shortcodes

Shortcodes are great, but if it’s provided by your theme, then you may want to think twice about excessively using it. Why?

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.

Tip 3: 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:

1
2
3
4
5
function my_shortcode_function() {
$i = '<p>Hello World!</p>';
return $i;
}
add_shortcode('my-shortcode', 'my_shortcode_function');

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:

[my-shortcode]

Useful guide: how to create a shortcode in WordPress.

Tip 4: Using Shortcodes in Widgets

Using a Shortcode in WordPress 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 or a site-specific plugin.

1
add_filter('widget_text', 'do_shortcode');

Tip 5. 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.

Let’s assume you have created a custom page template, and you want to include a shortcode to display a contact form. Simply add your shortcode, like this:

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

Tip 6. 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.

1
add_shortcode( 'shortcodetag', '__return_false' );

This code adds back the orphan shortcode with no output. Don’t forget to replace shortcodetag with your shortcode name.

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
function wpb_find_shortcode($atts, $content=null) {
ob_start();
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>
    <?php
    }
        echo '</ul>';
} else {
        echo "Sorry no posts found";
}
wp_reset_postdata();
return ob_get_clean();
}
add_shortcode('shortcodefinder', 'wpb_find_shortcode');

This code simply creates a shortcode called shortcodefinder. It runs a WordPress query and lists posts with a given shortcode tag.

For example, if you wanted to find all posts containing shortcode [contact-form-7 404 "Not Found"] then you would simply enter [shortcodefinder find=’contact-form’] in a WordPress page and save it. Now if you preview the page you will be able to see all the posts containing the shortcode.

For more detailed instructions checkout our guide on how to find and hide unused shortcodes in WordPress.

Bonus Tip: Which Shortcodes Can I Use right Now?

Often users don’t know which shortcodes are available to them. The shortcode reference plugin

provides a list and details about available shortcodes in your WordPress site.

We hope these tips helped you learn how to use shortcodes and make the most out of them in WordPress like a pro.

Download The WPSaviour App Now

Related posts
Beginner’s GuideBlogTips

Top 5 free payment gateway plugins for WordPress in India (2020)

Beginner’s GuideBlogTips

Best WooCommerce International Payment Gateways for WordPress in 2020

Best WordPress PluginsBlog

WordPress 5.4 Will Add Lazy-Loading to All Images

Beginner’s GuideBlog

How to Add a Preloader Animation to WordPress (Step by Step)

Sign up for our Newsletter and
stay informed

Leave a Reply

Your email address will not be published.