How to use do_shortcode in WordPress

Extending the functionality of your website is easy with the help of shortcodes. To place them anywhere on your website, you can use the function do_shortcode of . Here we will show you how to use it. Let’s start by exploring the basics of shortcodes!

What is shortcode called?

WordPress shortcodes add extra functionality that allows you to embed content in pages, posts, and sidebars. A lot of themes and plugins use shortcodes to better dynamically add content to your site. To make it clear how a shortcode works, let’s use the Contact Form 7 plugin shortcode:

As you can see it is quite simple. We have the Contact Form 7 macro inside the square brackets. The macro is generated by the Contact form 7 plugin. WordPress replaces the macro with content that is produced by a PHP function. If we add this shortcode to a post, page or sidebar, WordPress has to display our contact form.

But what if you want to use the WordPress shortcodes within the theme files? For that you can use the function do_shortcode. Next, we will take a closer look at how to use this function.

What do you need?

Before continuing with this tutorial make sure you have the following:

  • Access to your WordPress admin dashboard

Using do_shortcode in WordPress

Now that you have a better idea of ​​how shortcodes work in WordPress, we’ll move on to show you how you can use shortcodes to enhance the functionality of your site.

For example, let’s assume you want to include the Contact Form 7 plugin somewhere in the header of your site. But the problem is that you don’t have any widget in that area of ​​your theme.

See also  Web Pro files

First of all, you will need to get the shortcode of the plugin you want to use. For example, to get the Contact Form 7 shortcode you have to follow these steps:

  1. Log in to the WordPress admin dashboard, select Contact > Contact Forms.
  2. You’ll find the shortcode on the right side of the form name:
  1. Double click on the shortcode and copy it.

Since your theme doesn’t have any widgets in the header and we can’t just copy the shortcode into the header, we’ll proceed to use the function do_shortcode of WordPress. It will look like this:

Now to add Contact Form 7 to the header we will need to edit the header.php file. Before proceeding we recommend that you make a backup of your theme files that you are going to alter and a backup of your WordPress.

In the following steps we will show you how to edit WordPress files with the file manager, although you can do it using an FTP client, or from the Appearance > Edit menu in the WordPress administration dashboard:

  1. Log in to the Control Panel and select File Manager.
  2. Inside the administrator you have to locate the WordPress installation directory. It is usually found installed in public_html. Now navigate to wp-contents > themes folder.
  3. You will find all the themes you have installed, select the theme you are using and open it.
  4. Since we are going to edit the header, open the header.php file.
  5. Scroll the page and select the place and div where you want to add the shortcode. Our do_shortcode function should look like this:
See also  HTTP vs HTTPS: Comparison, Pros and Cons, and More

  1. Now save the file and check how your website looks like.

In most cases editing your theme files can break your theme. It is quite certain that you will also have to alter the to keep your WordPress looking good.

conclusion

You have now learned how to use shortcodes and display them with the WordPress do_shortcode function. Simply drop it into your theme file and you’re good to go. Don’t forget to backup your site before editing the file to avoid the risk of data loss.

Do you have any questions about the topic? Leave a comment below!

Gustavo is passionate about creating websites. He focuses on the application of SEO strategies at for Spain and Latin America, as well as the creation of high-level content. When he is not applying new WordPress tricks you can find him playing the guitar, traveling or taking an online course.

Loading Facebook Comments ...
Loading Disqus Comments ...