Using do_shortcode to execute shortcodes in WordPress

The WordPress do_shortcode function is used to parse and execute shortcodes within a given content string. Shortcodes are special tags that allow users to include dynamic content or functionality in their WordPress posts or pages without needing to write code. The do_shortcode function can be useful for developers and users who want to programmatically process and render shortcodes within their WordPress themes or plugins.

By using the do_shortcode function, developers can easily parse and execute shortcodes within their custom templates or functions, allowing for dynamic and customizable content. This can be particularly useful for creating reusable components or adding dynamic functionality to WordPress websites without having to manually write and process shortcode logic.

  • It can be used to render custom shortcodes within custom theme templates
  • It can be used to process and execute shortcodes within custom plugin functionality
  • It provides a convenient way to dynamically include content or functionality within WordPress posts or pages

Parameters accepted by the do_shortcode function

  • $content (string, required): Content to search for shortcodes.
  • $ignore_html (bool, optional, default value: false): When set to true, shortcodes inside HTML elements will be skipped.

Value returned by the do_shortcode function

The function returns a string which is the content with shortcodes filtered out.


How to use do_shortcode to output a simple shortcode

echo do_shortcode('[example_shortcode]');

This code snippet uses the do_shortcode function to output the result of a simple shortcode [example_shortcode].

How to use do_shortcode within a WordPress loop

if (have_posts()) {
 while (have_posts()) {
 echo do_shortcode('[custom_shortcode]');

This code snippet demonstrates using the do_shortcode function within a WordPress loop to output the result of a custom shortcode [custom_shortcode] for each post.

How to use do_shortcode with a shortcode containing attributes

$atts = array(
 'param1' => 'value1',
 'param2' => 'value2'
echo do_shortcode('[custom_shortcode att1="' . $atts['param1'] . '" att2="' . $atts['param2'] . '"]');

This code snippet showcases using the do_shortcode function with a custom shortcode [custom_shortcode] that contains attributes att1 and att2 with respective values value1 and value2.


In conclusion, the do_shortcode function is an effective utility for developers working with WordPress. It allows for the execution of shortcodes within PHP files, providing flexibility and customization for content display. By understanding how to properly use and implement this function, developers can enhance the functionality and user experience of their WordPress websites. With its ability to dynamically process and render shortcodes, do_shortcode is an essential function for any developer looking to create dynamic and interactive content within their WordPress projects.

Related WordPress Functions