WordPress Tutorial

How To Use WordPress do_shortcode

WordPress do+shortcode

Introduction

WordPress shortcodes provide you extra functionality and allow you to embed content into pages, posts, and sidebars. Many plugins and themes use shortcodes and empower users to dynamically add content to their blogs. To better illustrate how shortcodes work, let’s take Contact Form 7 shortcode as an example:

As you can see it’s pretty simple. We have Contact Form 7 macro in the square brackets. Macro is generated by Contact From 7 plugin. WordPress replaces the macro with content which is produced by a PHP function. If we add this shortcode to post, page or sidebar, WordPress would show our contact form.

But what if you you want to use WordPress shortcodes in theme files? Well, you can do this using WordPress do_shortcode function.

Let’s delve deeper and see how to use WordPress do_shortcode function.

[optin-monster-shortcode id="cgx87nzwxn0himxw62bn"]

What you’ll need

Before starting this tutorial you’ll need the following:

  • Access to your WordPress admin area

This tutorial shows how to edit WordPress theme files. Thus before starting, we strongly recommend to make a full backup of your WordPress site and using WordPress child theme.

Using WordPress do_shortcode

Now, that you have a basic idea of what shortcodes in WordPress are, let’s move ahead see how can you use WordPress shortcodes to boost your website’s functionality.

For instance, let’s assume that you want to include Contact Form 7 WordPress plugin somewhere in your theme header. But the problem is that you do not have any widgets there.

First of all, you would need to get the shortcode of the plugin you want to use. As an example, we will use the same Contact Form 7 shortcode:

  1. Access WordPress dashboard and click on Contact -> Contact Forms.
  2. You will find the shortcode for your form on the right side, near form title:
    WordPress Contact Form 7 Shortcode
  3. Double click on the shortcode and copy it.

Since our theme does not have widgets in header and we cannot simply copy and paste the shortcode, we will need to use WordPress do_shortcode function. It looks like this:

Now to add Contact From 7 to our header, we will have to edit header.php file. As mentioned before, it’s strongly recommended to backup your WordPress before altering any WordPress core files and use WordPress child theme.

The following steps show how to edit WordPress files with Hostinger File manager, however, the same result can be achieved using FTP client (click to see tutorial):

  1. Access Hostinger control panel and click on File Manager
  2. Next, locate the WordPress installation folder (usually, it’s public_html) and navigate to wp-contents -> themes folder.
  3. Find your currently active theme folder, and open it.
  4. Since we want to add contact form into the header, open header.php.
  5. Scroll down the page and select the exact location and div wherein you want to add the form. Our WordPress shortcode with do_shortcode function will look like this:
  6. Save the file and check how your website looks. 
    Add WordPress do_shortcode to header.php

Note: In most cases, editing theme core files will break your WordPress template. Most likely you will have to alter CSS to make WordPress look good. Be sure to check our tutorials on how to inspect CSS styles with browser and download CSS cheatsheet.

Conclusion

We hope, by now you understood the sheer usefulness of shortcodes and their ability to boost the look and functionality of your website. We hope that the above tutorial was easy to follow and you now know how to use WordPress do_shortcode function. Still, if you have queries, do let us know in the comments section below.

Join thousands of subscribers worldwide

and get awesome deals & the newest tutorials to your email

Please wait...

Thank you for sign up!

More in WordPress Tutorial
How to Fix The site ahead contains harmful programs Error on WordPress
How to Fix The site ahead contains harmful programs Error on WordPress

Close