Header Text - Tips & Tricks for Using WordPress do_shortcode

In WordPress, shortcodes are the quick tools that help you add special features, such as forms or sliders. They work well in posts and pages, but sometimes, you may want them to appear in other parts of your site, like the header or footer. That’s where the WordPress do_shortcode comes in.

It helps you place shortcodes anywhere on your site, even outside the main content areas. This tutorial shows you how to use do_shortcode to customize your site. We also cover best practices, tips, and tricks to enhance do_shortcode usage.

Lastly, we go through some common mistakes to avoid. By the end, you’ll know how to make your site more flexible and feature rich.

KEY TAKEAWAYS

  • WordPress do_shortcode lets you use shortcodes outside posts and pages, making your site more flexible.
  • Limit hardcoding shortcodes in templates to keep your site easier to manage.
  • Always back up your site before editing theme files and use a child theme to preserve changes during updates.
  • Test custom shortcodes for compatibility before using them on live pages.
  • Use caching plugins for resource-heavy shortcodes to improve site performance.
  • Follow security best practices by sanitizing data passed to shortcodes.
  • Combine shortcodes, create custom ones, or use conditional logic to make your site more dynamic.
  • Widgets or blocks can sometimes be better than do_shortcode for certain areas.
  • With careful usage, do_shortcode can unlock powerful customization options for your WordPress site.

What Is do_shortcode Function?

Before we dive into the wp do_shortcode function, let’s discuss what shortcodes are. Shortcodes are small bits of code in square brackets, like [shortcode]. They allow you to add certain features, such as forms or galleries, without writing any programming code. You can easily place them in posts, pages, or widgets to make your site more dynamic. 

However, one can only use shortcodes in the main content areas like posts and pages. If you want a slider in the header or a contact form in the footer, standard shortcodes fall short – they aren’t built for these areas.

The wp do_shortcode function fixes this limitation. It allows you to use shortcodes throughout your website, like the header, footer, sidebar, or anywhere within the content. This makes your site more flexible and encourages creative design options.

Here’s how you use it: 

<?php echo do_shortcode('[your_shortcode]'); ?>

This PHP code tells WordPress to process the shortcode and display the feature wherever you include it in your site’s template files, like header.php or footer.php. With do_shortcode, you can take full control of where your shortcodes appear.

How to Use WordPress do_shortcode?

To use do_shortcode, you need to access theme files based on where you want to add dynamic features to your website. You can access theme files in the following 3 ways:

  1. Using WordPress Admin Dashboard
  2. Using Your Hosting’s File Manager Feature
  3. Using an FTP Client.

In this tutorial, we use the first method. However, we’ll provide guides for the second and third approaches to help you learn how to use File Manager and how to configure, manage, and upload files to your FTP client.

Whatever method you use to access your theme files, adding a WordPress do shortcode will be the same, based on your requirements.

Let’s assume you want to add a sign-up form after the blog post’s content – ensure you have installed the WPForms plugin and have created your form. Once done, go through the following steps:

Click on WPForms in your dashboard menu. Navigate to your required form and copy its shortcode.

WordPress do_shortcode - Copy Forms Shortcode

Next, go to Appearance Theme File Editor. Under the Theme Files menu, head to template-partscontent content-single.php. Now, add your WordPress do_shortcode function as follows. Ensure it’s added within the PHP tags (<?php ?>):

echo do_shortcode('[wpforms id="1844"]');

Then, click Update File to save changes.

WordPress do_shortcode - Add Do Shortcode In Theme Files

This is how it will look on individual posts:

WordPress do_shortcode - Form Added Using do_shortcode

You may also edit the content-single.php file to add a heading to let users know what to do with this form and why it is there. To do that, add a heading as follows:

WordPress do_shortcode - Add Heading

Hosted.com prioritizes ease of use for developers with our WordPress Hosting plans that come equipped with WP-CLI, SFTP, Shell Access, and GIT support to simplify your development process.
Take advantage of these powerful tools to create and maintain exceptional sites.

Now, the form will be displayed as follows on the front end:

WordPress do_shortcode - Add User Friendly Form

If you want to access your theme files using File Manager, you can refer to the following guide to learn how to use the Hosted.com File Manager:

How To Use cPanel File Manager

To easily access your file, use the File Manager search tool:

WordPress do_shortcode - Search for a Specific File

However, if you’re using FileZilla – an FTP client – then you may look at the following:

How To Configure A Site In FileZilla

How To Manage Files In FileZilla

How To Upload Files Using FileZilla

Best Practices & Considerations

Before you edit any theme files, always backup your website. Backups ensure you can restore your site if something goes wrong. You can use a plugin like UpdraftPlus or your hosting provider’s backup tools to save your site’s data.

TIP:

If you’re a Hosted.com user, refer to “Use Hosted.com to Export WordPress Site” to back up your site using the built-in tools of the Hosted.com control panel.

When adding customizations, use a child theme. A child theme keeps your changes safe, even if the main theme is updated. Without a child theme, your edits can get erased during theme updates, causing you to lose your work.

Ensure you consider the potential impact on site layout, as adding shortcodes with the do_shortcode function can sometimes change your site’s design. For example, inserting a slider may overlap with existing elements. To fix this, you should adjust your CSS to ensure everything looks neat and works well across devices.

Tips for Optimal Usage 

Using WordPress do_shortcode too often can slow down your site, especially if the shortcodes are complex or run on every page load. Instead, try alternatives like WordPress website widgets or custom blocks, which are more efficient and easier to manage in many cases. This approach keeps your site fast and responsive. 

Always test custom shortcodes before adding them to your theme files. Place them in a draft page or post to check if they work as desired. This ensures everything looks good and functions properly before your site goes live. 

If your shortcode creates something resource-intensive, like a dynamic gallery or a product feed, use a caching plugin like W3 Total Cache. Caching minimizes the load on your server and makes your site run smoother for visitors. 

However, if a shortcode isn’t working, check for typos or missing parameters. Debugging tools like the WordPress site debug log and plugins like Query Monitor identify errors. Troubleshooting ensures your shortcodes are always functional and error-free. 

Tricks to Enhance do_shortcode Usage 

Here’s how you can improve WordPress do_shortcode usage:

Create Custom Shortcodes 

For something unique, use the add_shortcode function to build your own. For instance, add the following function in the functions.php file to create a custom shortcode to display a greeting: 

function custom_greeting() {
    return "Welcome to our site!";
}
add_shortcode('greeting', 'custom_greeting');

Then, use it with WordPress do_shortcode wherever you want to greet your user: 

echo do_shortcode('[greeting]');

This allows you to adapt features to your specific needs.

Combine Shortcodes 

You can create more powerful outputs by nesting shortcodes inside each other. For example, you can combine a gallery shortcode with a column layout. To do this:

Check if [columns] shortcode exists. This shortcode is likely custom, so it may already exist in your theme or a plugin. Search in your theme’s functions.php file or the documentation of your installed plugins.

If not available, add the following code to your theme’s functions.php file to create the [columns] shortcode:

function columns_shortcode($atts, $content = null) {
    // Optional: Parse attributes if needed
    $atts = shortcode_atts(array(
        'class' => 'default-columns',
    ), $atts);

    // Wrap content in a div for column layout
    return '<div class="' . esc_attr($atts['class']) . '">' . do_shortcode($content) . '</div>';
}

add_shortcode('columns', 'columns_shortcode');

This code creates a basic [columns] shortcode that wraps content (like the gallery) in a <div> for styling.

Then, go to Media Library in your WordPress Dashboard and click on the images you want to use in the gallery. For each image you open, check the URL; the item= parameter contains the image ID (e.g., https://www.yourdomain.com/wp-admin/upload.php?item=1781, where 1781 is the ID).

Now, open your theme file where you want to use WordPress do_shortcode and add the following statement. Don’t forget to update the ids attribute with your image IDs:

<?php echo do_shortcode('[columns][gallery ids="1781,1782,1779"][/columns]'); ?>

This method helps you create complex designs or features without extra plugins, keeping your site clean and functional.

Use do_shortcode with Conditional Logic 

You can make your site smarter by showing shortcodes only when certain conditions are met. For example, display a message for logged-in users: 

if (is_user_logged_in()) {
    echo do_shortcode('[your-welcome-shortcode]');
}

This approach personalizes user experiences and keeps your site dynamic.

Integrate do_shortcode in Plugins 

If you’re a plugin developer, WordPress do_shortcode can help you add flexible functionality. For example, you could use it to embed a feature like a booking form directly into templates. This way, users can place your plugin’s features wherever they need.

Common Mistakes to Avoid 

While do_shortcode is powerful, overusing it in your theme templates can make your site harder to manage. For example, if you hardcode shortcodes in multiple files and later need to update them, you’ll have to edit each file individually. This can be time-consuming and prone to errors. Instead, try to limit hardcoding and centralize your shortcode usage.

When using do_shortcode, always follow security best practices. Ensure that any data passed to your shortcodes is sanitized and escaped. For example, if you accept user input, use sanitize_text_field() or similar functions to prevent malicious code from affecting your site. A secure site protects both you and your visitors.

Sometimes, using blocks or widgets may be more appropriate than relying on WordPress do_shortcode function. For example, if you want to add a YouTube video to your sidebar, a widget or block offers a more flexible and user-friendly option. Reserve do_shortcode for cases where these alternatives don’t work. 

Strip Banner Text - Developer-friendly WordPress Hosting plans that support WP-CLI, SFTP, Shell Access, and GIT. [More Info]

FAQs

Can I use WordPress do_shortcode with custom shortcodes?

Yes, you can! First, create a custom shortcode using the add_shortcode function. Once it’s ready, you can use do_shortcode to display it anywhere on your WordPress site, even in theme templates.

Can I use do_shortcode with custom themes?

Yes, do_shortcode works seamlessly with custom themes. You can add it to any PHP file in your WordPress website theme to display shortcodes wherever you use them.

Does using do_shortcode slow down my site?

It can, especially if you overuse it or use shortcodes that perform heavy tasks. To avoid slowing down your WordPress site, limit the number of shortcodes in templates and consider caching for resource-heavy shortcodes.

Are there security risks with do_shortcode?

If you don’t use it carefully, WordPress do_shortcode can pose security risks, especially when handling user input. Always sanitize and escape any data passed to shortcodes to keep your WordPress website secure.

What’s a better alternative to do_shortcode for adding features?

Sometimes, widgets, blocks, or plugins are better options than do_shortcode. For example, if you want to add a contact form to a sidebar, a widget or block is easier to manage and doesn’t require editing your WordPress site’s theme files.

WooCommerce Shortcodes: A Guide to Their Uses & Benefits

How to Fix the WordPress 500 Internal Server Error

WordPress Malware Removal: Manual & Automatic Methods

WordPress Debug Mode: How to Enable It & Fix Errors

How to Fix WordPress Stuck In Maintenance Mode