Tips on Adding Shortcode in WordPress

0
1821

If you are using WordPress, you may know the need to add shortcode in it. Whether it is for adding a customized content or offering a Facebook widget, the need for using shortcode may arise on various occasions. As WordPress is a versatile platform, it allows you to simplify the procedure of executing desired tasks through shortcodes.

In this tutorial, we are going to illustrate the method of adding a shortcode on your WordPress site. But before that, it’s time to discuss what shortcode is.

What is Shortcode?

what is shortcodeHave you ever come across the code that turns into another content when viewed? If yes, it must be a shortcode, as this special tag changes its content in a post when a visitor reads your post. In simple words, WordPress developers use a special format of coding through which a special content (such as gallery or customized widget) appears in the front end of a post.

For instance, when you enter

in a post, your visitors do not see ”

” text. Instead, they find a list of images present in your gallery. This is because the

shortcode guides your WordPress blog to display the site’s gallery. In a similar manner, you can enter a shortcode to display anything on your posts. You can add gallery, Twitter widget, a YouTube video and much more through shortcodes on WordPress.

Now being a modern blogger, you may be looking to add custom AdSense ads to your posts. The traditional WordPress technique will state you to turn your post editor to HTML mode and insert a code block at relevant place. But this also invites extra markup in posts and looks messy while editing. By using a shortcode, you can simply insert the following line of code in your post for adding an AdSense ad. In addition to this, you don’t need to edit every post individually when you want to change your ad block.

How to add shortcode in WordPress?

If you are convinced with shortcodes and are looking forward to add them in your WordPress posts, here is the procedure. It should be noted that we are using the AdSense ad example to illustrate the steps.

To start with, you should log into your WordPress site and navigate to your theme files. Here, you must find the functions.php file and open it using an advanced text editor. This is important because you have to insert a code and AdSense ad function into the file. You can use the following function in the theme file for displaying an AdSense ad effectively.

Add Shortcode in WordPress 1

Moreover, the basic function of the above-mentioned code is to return whatever code is used in it as a string. For example, if you replace the AdSense code with a Facebook widget code, it will display the Facebook widget in your posts. Hence, you can add desired string in your site’s theme file for offering custom content on your posts.

Though you have modified the functions.php file in your theme, the task is not fully done. The main task is still left to be done. Now you need to insert the shortcode in your posts where you want to display an AdSense ad. For this, you should create or edit a post and navigate to the location where you want the ad to be displayed. In the desired location, you have to insert the following shortcode.

Add Shortcode in WordPress 2

Once done, you should save your WordPress post. Now you will notice that the AdSense ad has been successfully added to your post. With that said, you can add any kind of content on your site. All you need to do is to edit the functions.php file and add shortcode in WordPress posts.