Want to add some new and interesting functionality to your WordPress post or page? You don’t need to write any code for the purpose! Now, you can add functionality using shortcode, a tool that most plugins and widgets on WordPress tend to use. Before trying to understand what are the different ways in which you can add shortcodes to your post or page, let us try and understand what shortcodes are.

Whenever you try to add content to WordPress, the content goes through many security checks. The whole process is done to ascertain there is no malicious code in the content. But, there are times you don’t want to use a code or go through this process. This is where shortcodes come to your aid. They directly fit the content in its place, and you have the chance of avoiding code and the whole process of checking.

Let’s try and see tips on how the various codes can be integrated into your WordPress post or page.

Avoiding the Shortcodes

Tip number one on shortcodes is to remember that shortcodes need not be used every time on your post or page. You need to know when best to avoid shortcodes, and when to use them. Let’s assume you have used a shortcode-based wordpress theme. Now, if you tend to use all the shortcodes, then you cannot change the theme unless the new theme is also shortcode-based. While using a theme-specific shortcode is a great idea, you should always have your eyes on CSS buttons on WordPress, just in case you plan to change your theme.

Instead of using shortcodes and then implementing the necessary banner or text or CSS button every time, you can use plugins for some of the shortcodes.

Using Site-Specific Plugins

While shortcodes are a brilliant way of getting your work done on the page or post, you may want to rethink in case you are using shortcodes that are specific to a theme. What will happen when you change the theme? You don’t want to rebuild something that can be avoided. For this purpose, you should always use site-specific plugins. So, in case you change the theme, all you need to do is copy the code that is present in the function.php file of your theme, and paste it onto the site-specific plugin. This way you can use shortcodes and not worry about changing the theme.

Easy Search for Shortcodes

You must secure your shortcode for the future, just so that it does not bother you when you change the theme. This is why you need to know the function that is used in the shortcode and make sure you know how to search for it in your new theme.

Searching for the function of the shortcode in your new theme:

Open theme folder

wp-content/themes/your theme name

look for the function.php file. In case there is an includes folder, check it

search for the term add_shortcode in this file/folder

The code would look something like this
Function my_shortcode_function() {

$i = <p>Hello World! </p>’;

Return $i;

}

add_shortcode (‘my-shortcode’, ‘my_shortcode_function’);
Here you just created a shortcode that returns a certain value.

Shortcodes through Widgets

Widgets are probably the best things that WordPress has managed to create. You would see that using widgets is not just fun but also very easy. You don’t need to restrict the use of shortcodes to posts and pages. The best part is that you can use shortcodes to create and build new widgets for your WordPress site.

To use shortcode through the widget, you just need to drag and drop the particular widget to the side/top/bottom bar. Once done, add the shortcode to the widget, specify the size and save it.

Remember, adding the shortcode to the widget is not a default function, and you will not find it easily. You need to make sure that the shortcode function is added. In case it is not, you can add the following code to the function.php file of your theme.
add_filter (‘widget_text’, ‘do_shortcode’);

Use Theme Files to Add Shortcodes

Let’s say you don’t want to use it in any widget. Well, you can do that as well. You just need to add the shortcode to a non-widget part of the theme file.

Create a custom page template for the same. Now let’s assume you want to create a custom page “contact form”. You just need to add the following shortcode to your theme file.
<?php echo do_shortcode (“[example_shortcode]”); ?>

Fixing Broken Codes

When you use shortcodes, you need to keep in mind that there are chances that the shortcode does not work when you shift themes. Have you ever considered that there could be a broken link or code when you shift themes?

There are times when you get to know about a broker code only when someone points it out to you. So what will you do in that case? This is where you will need to fix the broken code and make the shortcode workable once again.

There are two ways of performing the fix

  • Visit the places on your site manually, where you have used the shortcode, and remove them one by one
  • Hide the shortcode. To hide the broken shortcode, you just need to add the following code in the function.php file of your theme

dd_shortcode (‘ shortcodetag’ , ‘__ return_false’ );
with this added to the shortcode, there will be no output. The shortcode tag in the example is actually the name of the shortcode. You need to replace it with the name you have provided the shortcode

Shortcodes within Posts

Let’s say you want to remove all the shortcodes within the post manually. You will first need to find the shortcodes on your post/page.

Create a shortcode finder using a code. You need not create such codes, they are readily available online. Once you add this to your site, it will run a query, and you will see it return the shortcode tag.

These are some of the various tips that can help you use shortcodes on WordPress with ease.