WordPress do_shortcode

WordPress do_shortcode

WordPress do_shortcode

A WordPress do_shortcode function looks like this <? php echo do_shortcode( ‘[ shortcode ]’ ); ?>. But, what are they? How are they used in WordPress? Are they different from WP shortcodes? We’ll address these questions soon.

To start with, WordPress shortcodes are enormously valuable. They help to extend WordPress functionality and aid ease of use. For instance, when you create a signup form or a slider, shortcodes let you display them on your site.

However, shortcodes have their limitations. They can only be added to the body of your page or post. So, how can you embed content on your site’s header file or footer? This is where WordPress do_shortcode comes in!

With this function, you can place your shortcodes anywhere on your site. Although using this feature requires WP file editing, we bet you’ll find it easy.

In this guide, we’ll take you by the hand to show you how to explore the versatility do_shortcode function presents.

Content:

What are WordPress Shortcodes (and how they work)

WordPress introduced shortcodes in version 2.5. They are small code snippets (or shortcodes) that perform specific functions when added to a WordPress site. They are more like shortcuts or timesavers. Instead of writing long HTML codes, you can simply copy and paste a WP shortcode to perform the same function.

Let’s see how this works using a contact form plugin (you can use a gallery plugin or a youtube embed plugin and achieve similar results).

Contact form 7 can create beautiful, user-friendly forms for WordPress sites. Each time you design a form with the plugin, it generates a shortcode.

contact form shortcode sample

To display the form on your front-end, copy and paste that shortcode on your page or post (in your WP editor).

how to add shortcodes to a post

Don’t forget to save the post.

contact form

Now, your site visitors can view and fill the form’s custom fields. So simple, right?

This is just one of the many instances where shortcodes can be used on your website. Most WordPress developers have adopted it to save time and effort.

You can add a video gallery, video embed, event calendar, pricing table, etc via this method. But, as we said earlier, there is a limit to where content can be placed with shortcodes.

You can only go as far as placing the shortcode within your post or page. And this is where WordPress do_shortcode function comes in. We’ll discuss that in greater detail in the next section.

Side note: Would you like to add a contact form to your WP site? We have compiled a list of the best contact form plugins. Check it out to know which is best for your site.

What is WordPress do_shortcode function?

Now that we have a grasp of how shortcodes work, let’s see how WordPress do_shortcode function works.

A WP do_shortcode function looks like this:

<?php echo do_shortcode( ‘[ paste your shortcode here]’ ); ?>

With this, you can enjoy the benefit your shortcode offers even more robustly. That means you have more control over where you place shortcodes and you can put them directly in a template file. To use the WordPress do_shortcode function, ensure that you have the following:

  • A complete backup of your website: Backing up your website could save you a lot of trouble just in case there is an error in WordPress. We have put together a WordPress tutorial to teach you how to backup your site.
  • Access to your server through FTP: Download and install an FTP client like FileZilla. With this application, you can connect to your site and interact with your files.

There are other ways of accessing your file manager. For instance, WordPress hosting providers allow you to have access to your files through the cPanel. But you should use FTP since it offers you more options.

Learn more by reading this step by step guide on how to use FTP.

How to use WordPress do_shortcode Function?

Using this shortcode function involves opening a PHP file and editing it, but it isn’t difficult. Follow these three steps to add content to your header (or footer).

Step 1: Find the shortcode 

You can use any shortcode you want, probably one from a plugin. Just ensure that the content you want to embed is a perfect fit for its location.

Step 2: Open the theme file you want to edit

Note: It’s best to use a child theme to avoid loss of data. Read this complete guide on how to create a child theme. To get started, access your WordPress root directory via FTP. We’ll use FileZilla as our FTP client in this sample.

Open file >> site manager at the top-left corner of your screen.

FTP

Next, fill in your site’s details (same data you use to log into your admin panel) and click ‘connect’. You may get these details from your host if you do not have them.

WordPress do_shortcode

Once connected, open your main WordPress folder (public_html in many cases).

WP main folder - WordPress do_shortcode

Within this folder, locate your theme folder (wp-content>themes>yourtheme), and open it.

Where would you like to paste your add the shortcode? Your theme header?

If yes, simply scroll through the files in your theme folder, identify the header.php file and right-click.  Next, select View/Edit.

header file - WordPress do_shortcode

Side note: To add the shortcode to your theme’s footer, edit the footer.php file instead.

Step 3: Add the WordPress do _ shortcode function to the PHP file

Here is a screenshot of our header.php file.

header file php - WordPress do_shortcode

Paste the do_shortcode at the exact location where you want your content to be. Here is an example of a contact form 7 shortcode you can add:

<?php echo do_shortcode(“[contact-form-7 404 "Not Found"]”); ?>

When pasted, save and close the file.

Back in FileZilla, you will be prompted to confirm if you want to replace the existing version of the file. Click Yes.

Finally, check your site to view the changes you have made.

Bonus: How to create Shortcodes in WordPress Websites (Advanced Users)

Besides using premade shortcodes, you can also create yours to perform selected functions.

Let’s say you often add subscribe notes to your posts, you can use shortcodes to ease the process. Instead of writing a chunk of texts each time, pasting a shortcode created for this purpose will do the job.

Here is how it works:

You create a callback function that runs every time you use the shortcode and then connect that function to a particular shortcode. Let’s see an example. We’ll create a shortcode for creating subscribe notes on our WP posts.

First things first, edit your theme’s functions.php file.

You can find the file here: /wp-content/themes/yourtheme/functions.php. Then we add these lines of code.

//to add subscribe text to my posts

function subscribe_shortcode() {

return ‘subscribe and join thousands of people to receive weekly WordPress tutorials’;

}

add_shortcode(‘subscribe’, ‘subscribe_shortcode’);

If you don’t understand the above lines of code, don’t worry. We’ll explain them line by line.

The first line is just a comment to help us understand the code later on. You can skip it.

// to add subscribe texts to my posts

Define your function in the next line of code. It’s best to use a name that relates to the purpose of our shortcode. In this context, we’ll use ‘subscribe_shortcode’.

function subscribe_shortcode() {

The next line of code defines our message. When there is a shortcode callback, this message will be displayed.

return ‘subscribe and join thousands of people to receive weekly WordPress tutorials’;

Close the function with a bracket.

The last line of code defines the shortcode itself (through the add_shortcode function). There are two variables here: the first one defines the shortcode we’ll use while the other calls the function we defined previously.

add_shortcode(‘subscribe’, ‘subscribe_shortcode’);

Finally, save the functions.php file. Henceforth we can call our message anytime by placing the shortcode tag in our WordPress editor.

Every time you type this:

[subscribe]

This following message appears on your front-end:

‘subscribe and join thousands of people to receive weekly WordPress tutorials’

Feel free to input your message.

Final Thoughts – WordPress do_shortcode

Now you have it. You have learned how to use shortcodes, do_shortcodes, and how to add the functions to your PHP file. We established that pasting shortcodes inside your WP editor can save you from writing too many HTML codes. Do_shortcodes goes a step further to cover for the inflexibility of shortcodes.

Simply edit your template files and include the do_shortcodes. Such a relief to most WordPress users.

What questions do you have? Let us know in the comments below. We’ll be glad to help.