×

Hupspot Guide to WordPress do_shortcode

Hupspot Guide to WordPress do_shortcode

The Hubspot style of technical documentation focuses on clear steps, examples, and best practices. In that spirit, this guide walks you through how to use the WordPress do_shortcode() function to render shortcodes anywhere in your site, not just inside regular post content.

By the end, you will know exactly how to execute shortcodes in templates, widgets, blocks, and more, while avoiding common mistakes.

What WordPress do_shortcode Does

In WordPress, shortcodes are small tags wrapped in square brackets, like or

← Back

Thank you for your response. ✨

. Normally, they are processed only inside post or page content.

The do_shortcode() function lets you run those same shortcodes manually, inside PHP, so you can place them in page templates, sidebars, or other custom locations.

In simple terms, do_shortcode() takes a string that contains a shortcode and returns the HTML output generated by that shortcode.

Basic Syntax Explained in a Hubspot-Style Walkthrough

The basic usage of do_shortcode() looks like this:

<?php echo do_shortcode( '[shortcode-name attribute="value"]' ); ?>

Key points:

  • You pass a string that includes your shortcode.
  • The function processes the shortcode and returns the generated content.
  • You typically wrap it in echo so the result appears on the page.

Where You Can Use do_shortcode in WordPress

Following a Hubspot-like structure, let’s break down the main places you might need do_shortcode() and how to implement it safely.

1. Using Hubspot-Friendly Shortcodes in Theme Templates

Theme template files (for example, page.php or single.php) are common places to run shortcodes directly. Open the relevant template file and insert PHP where you need the output.

Example:

<?php echo do_shortcode( '[contact-form-7 id="123" title="Contact"]' ); ?>

Steps:

  1. Locate the template file in your theme directory.
  2. Open it in a code editor.
  3. Place the echo do_shortcode() line where you want the shortcode output to appear.
  4. Save and upload the file if needed, then refresh your page.

2. Running Shortcodes in Widgets

Out of the box, text widgets in some themes may not process shortcodes. You have two main options to enable shortcode support in widgets.

Option A: Enable Shortcodes in Widgets Globally

Add the following code to your theme’s functions.php or a site-specific plugin:

add_filter( 'widget_text', 'do_shortcode' );

This lets you paste the shortcode directly into the text widget. WordPress then automatically applies do_shortcode when rendering the widget.

Option B: Use a Custom Widget or Plugin

Some plugins provide shortcode-ready widgets. You add the widget in Appearance > Widgets and configure any settings. The plugin handles do_shortcode() for you under the hood.

Using do_shortcode Inside Blocks

As WordPress has shifted to block-based editing, you may want to use shortcodes within blocks instead of classic content areas. A Hubspot-inspired best practice is to keep shortcodes simple and clearly documented for editors.

Shortcode Block

The editor includes a dedicated Shortcode block:

  1. Add a Shortcode block in the editor.
  2. Paste your shortcode, such as .
  3. WordPress processes the shortcode when the page is rendered; no manual do_shortcode() call is needed.

PHP in Block Templates

If you are building custom block templates in your theme, you can still use do_shortcode() in PHP-based parts of your theme, for example:

<div class="custom-block-area">
  <?php echo do_shortcode( '[my-custom-shortcode]' ); ?>
</div>

Creating Custom Shortcodes the Hubspot Way

To get the most from do_shortcode(), you may want to create custom shortcodes that output reusable components, such as CTAs, signup areas, or Hubspot-style forms.

Step-by-Step: Register a Custom Shortcode

  1. Open functions.php in your active theme or a custom plugin file.
  2. Add a callback function that outputs or returns HTML.
  3. Register the shortcode name with add_shortcode().

Example:

function my_hub_style_cta_shortcode( $atts ) {
  $atts = shortcode_atts(
    array(
      'title' => 'Default Title',
      'button_text' => 'Click Here',
    ),
    $atts,
    'hub_cta'
  );

  ob_start();
  ?>
  <div class="hub-cta">
    <h2><?php echo esc_html( $atts['title'] ); ?></h2>
    <a class="btn" href="#"><?php echo esc_html( $atts['button_text'] ); ?></a>
  </div>
  <?php
  return ob_get_clean();
}
add_shortcode( 'hub_cta', 'my_hub_style_cta_shortcode' );

Now you can use:

[hub_cta title="Join Now" button_text="Get Started"]

or call it directly with do_shortcode():

<?php echo do_shortcode( '[hub_cta title="Join" button_text="Start"]' ); ?>

Hubspot-Level Best Practices for do_shortcode

To keep your site performant and maintainable, follow these guidelines when using do_shortcode():

  • Limit heavy shortcodes: Avoid running complex or database-heavy shortcodes in loops or across many widgets.
  • Validate attributes: Sanitize and validate shortcode attributes inside your callback functions.
  • Escape output: Use functions like esc_html(), esc_attr(), and wp_kses() where appropriate.
  • Cache output: For expensive operations, consider caching the result using transients or object caching.
  • Document usage: Provide clear instructions for content editors on how and where to use each shortcode, similar to Hubspot documentation standards.

Common Errors When Using do_shortcode

Watch for these frequent issues:

  • Missing square brackets or incorrect shortcode syntax.
  • Calling do_shortcode() before the shortcode is registered.
  • Forgetting to use echo, so nothing appears on the page.
  • Conflicts with other plugins registering shortcodes with the same tag.

Further Learning and Helpful Links

To go deeper into WordPress shortcodes and the do_shortcode() function, explore the original tutorial that inspired this walkthrough on the Hubspot blog: WordPress do_shortcode guide.

If you want expert help building scalable shortcode systems and technical SEO structures similar to a Hubspot-grade setup, you can also visit Consultevo for consulting and implementation support.

Used correctly, do_shortcode() gives you flexible control over where and how shortcodes render on your site, letting you build reusable components, conversion-focused sections, and consistent layouts across your entire WordPress installation.

Need Help With Hubspot?

If you want expert help building, automating, or scaling your Hubspot , work with ConsultEvo, a team who has a decade of Hubspot experience.

Scale Hubspot

“`

Verified by MonsterInsights