Site icon Hip-Hop Website Design and Development

How to Add a Shortcode in a WordPress Website?

how to add shortcode in WordPress

It is very important to know how to add a shortcode in WordPress so that your content can be made easily and it can have multiple features to appear attractive to the readers.

One of the most underestimated features of WordPress is its shortcode. In the praise of user-friendliness and plugins, the value of shortcodes remains underappreciated.

It is these shortcodes that make WordPress user-friendly like no other platform.

What Is A Shortcode and Its Usefulness?

A shortcode in WordPress is a tag that you can place in posts, pages, and even widgets to show different functions associated with like. It is like calling a function by its name to execute the code in its body.

It is the same way as embedding an image in your post. Shortcodes are represented by square brackets [shortcodename] and you have to place the in the spot where you want them to appear.

As a back-end user, you will not be able to see how the shortcode is showing its part unless you go for a preview.

If you know a little bit of PHP coding, you can create your custom shortcodes and use them on all your posts to get the desired feature.

For example, you can create a shortcode to embed tweets in your post with awesome graphical design to attract attention.

Another popular example is that you want to place AdSense Ad segment in your posts, you have to copy the code all the time and then paste it. Instead, you can create a shortcode and use it to represent the code for the ad segment.

An example of how useful a shortcode is can be derived from Meta Slider. It is basically a plugin but it comes with its own shortcode that lets you add a slider to your posts or pages. Just imagine the kind of code you would have to write to show slider in a post.

Similarly, WP Google Maps comes with shortcodes to embed maps in your posts. With shortcodes, you can embed almost anything with a single word.

How To Add A Shortcode In WordPress?

It is very simple to add a shortcode in WordPress and this is where WordPress scores over others at least for the developers.

Step 1 – Add A Shortcode In WordPress

You need to go to Appearance for the Dashboard and click on Editor. Find the file functions.php and edit it. Go to its end and add the code you want to like the following.

function get_adsense($atts) {
return '

';
}

Remember, the function/method name can be anything and the code is up to you to achieve your desired feature.

Step 2 – Displaying The Created Shortcode

Unless you display the created functional code, it would not work or show anything. Therefore, add the following line to the next line where the previous code ended.

add_shortcode('ShowMeAd', 'get_adsense');

If you take a look at the function, you will see there are two parameters. Note that the first parameter is actually the label of the shortcode that you will be using.

Some people your by calling the method by its name but that won’t work. You can only call the shortcode by the first parameter to set in this code. Here the label ‘ShowMeAd’ is the shortcode [ShowMeAd] that you should be using in the posts or pages or even widgets. The second parameter is the name of the function you created.

How To Hide A Shortcode?

Sometimes, you may have a broken shortcode or a shortcode that you do not want to see momentarily. In such cases, you can hide the shortcode from displaying any output. It helps immensely for developers to debug a code. Add the following code in functions.php.

add_shortcode( 'shortcodetag', '__return_false' );

Some Essential Tips To Add A Shortcode In WordPress and Using It –

Shortcodes are like plugins, they should be less but useful. You cannot go on to use shortcodes all the time in all your posts.

Most of the free themes come with a few basic shortcodes and while premium themes have hundreds of them. There are certain places where direct code would be helpful than a shortcode or at times, plugins can come handier than a shortcode.

When Not To Add A Shortcode –

If you are thinking of using a feature on all your posts such as having a vote form at the end of every post, you should either use a plugin and better yet, a direct code in core PHP files.

This would ensure faster processing and less of manual work by not adding a shortcode all the time.

Plugins Over Shortcode –

As most of the themes have their exclusive shortcodes, you may be tempted to use them excessively to make your post attractive and unique.

But in the case of changing the theme in future, the shortcodes are unlikely to be available in the new theme and your posts will get disrupted.

Hence, it is better to create a plugin yourself with the code of the shortcodes and use them so that installing new theme won’t affect the post.

You can find the code of shortcodes in the functions.php file of your theme. You just need to copy the code and create plugins with the same.

But do not fill your post with too many shortcodes that are time-consuming to execute. For examples, if you make a post with too many video and slider embeds and other visual features, the post will take a lot of time to load. But the same cannot be said in the case of pages.

In web pages, you need to have multiple features which could only be achieved with shortcodes. But try to use as minimum as you can and try to follow a specific layout.

By following a specific layout, you can hard code it so that the codes get executed automatically without function calling feature in between the execution which is the case when we use a shortcode.

Even when you are not an expert in creating shortcodes, you can follow different tutorials and mix different codes to come up with personalized and customized shortcode for mind-blowing features in your posts.

The post How to Add a Shortcode in a WordPress Website? appeared first on SKT Themes.