Beginner's Guide for WordPress - Start your WordPress Blog in minutes.
Choosing the Best
WordPress Hosting
How to Easily
Install WordPress
Recommended
WordPress Plugins
View all Guides

7 Essential Tips for Using Shortcodes in WordPress

Last updated on by
Special WordPress Hosting offer for WPBeginner Readers
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 provied 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:

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.

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:

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

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.

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

If you liked this article, then please subscribe to our YouTube Channel for WordPress video tutorials. You can also find us on Twitter and Google+.


Editorial Staff at WPBeginner is a team of WordPress experts led by Syed Balkhi. Page maintained by Syed Balkhi.

WPBeginner's Video Icon
Our HD-Quality tutorial videos for WordPress Beginners will teach you how to use WordPress to create and manage your own website in about an hour. Get started now »

Comments

  1. akki says:

    hi nice post! how can i make a custom code for slider and make dynamic.i upload images from back end,

  2. Minhaj says:

    Hi,
    I want to put my shortcode in html hyper link in button or something.
    like my shortcode is [shortcode]
    and my html code is Dummy

    Then how will I put [shortcode] in html code and my html code is Dummy

  3. marie says:

    I have a problem with my short codes in that the text within the buttons does not look very professional as the first letter is only showing half. This is not with every one, just some of them. I would be most grateful for a suggestion

  4. Praveen says:

    Hello, I have a question about shortcodes. As I am running a WordPress website that is based on Online Practice Test / Quizzes. To provide these quizzes I am using a plugin and in that plugin I have to add the questions and answers for a quiz. After that I have to put that shortcode in the post.

    My Post Structure
    Title
    [Shortcode]

    then in SEO Title and Description by All in One SEO plugin.

    So my question is, using the only shortcode in all post is safe from the SEO and Google webmasters Guideline point of view ?

    • WPBeginner Support says:

      Yes, it is totally safe. The shortcode never appears in the HTML of your website. Like any other PHP funtion, your shortcode is processed by the server and returns the HTML output which is then displayed on your post/page.

  5. Giochi MMO says:

    There is a way to delete a specific shortcode, maintaining the text inside?

    For example: in this case [dropcap]A[/dropcap] I would like to eliminate the shortcode maintaining the “A”, or any other letter inside.

    Thanks!

  6. Kate Ford says:

    I am locked into using a specific theme for business reasons, and it
    lacks some bells and whistles that are necessary and must be used with
    short codes. While the short codes get the job done, there is is one
    element we have to keep in mind:

    Don’t overuse short codes for important text elements if it compromises search engine returns.

    We
    have two plug ins for short codes — one of these keeps the text
    elements right in the post, but the other generates the content in the
    plug in, and only the code is inserted into the post — so there is no
    actual TEXT associated with that post.

    For instance, we created
    some grid like photo galleries using a plug in that relies on short
    codes. The galleries and their captions are created inside the plug-in,
    but only the short code linking to it is published in the post. While
    the post itself has SEO fields to complete, the text (and links) from
    the photo captions actually doesn’t exist IN the post — it exists in
    the output created by the plug-in, which does NOT have SEO fields. When
    I need to edit the gallery, I don’t edit the post, because all it says
    is [Grid 123]. I edit the gallery in the plug-in setting.

    So,
    while it “looks great” on the post, we have to weigh what is most
    important: Does this post need a great looking gallery which may not
    give me any search engine returns, or is the TEXT for gallery more
    important and I need it to be crawled?

    We have a page with FAQ
    and used short codes to make an accordion style display. Toggling a
    question reveals the answer, and then it closes again. On this page, we
    used short codes, but we relied on a different plug in which keeps the
    text IN the post, and simply styles it the way we need. In that manner,
    the text remains part of the post, the word count is accurate, and it
    is crawled by search engines. The short codes merely provide the
    requested style.

    So bottom line, if you have content
    that’s important and should be crawled, avoid short codes if they store
    the information apart or separate from your post.

  7. Tony Franco says:

    Hi! Nice post! Please how can i make a shortcode to print an image?

    Thanks!

    • Gyan says:

      Hii Tony Franco.

      this is incomplete question. please specify where and which image you want to use. what context you are talking about . ?

Add a Comment

We're glad you have chosen to leave a comment. Please keep in mind that all comments are moderated according to our comment policy, and all links are nofollow. Do NOT use keywords in the name field. Let's have a personal and meaningful conversation.