The do_shortcode() WordPress PHP function searches content for shortcodes and filters these shortcodes through their corresponding hooks. If there are no shortcode tags defined, the content will be returned without any filtering, which might cause issues when plugins are disabled but the shortcode will still show up in the post or content.
Usage
Using the function can look something like this:
// Use shortcode in a PHP file (outside the post editor). echo do_shortcode('[gallery]');
Parameters
- $content (string, required): This is the content to search for shortcodes.
- $ignore_html (boolean, optional): When set to true, shortcodes inside HTML elements will be skipped. Default is false.
More information
See WordPress Developer Resources: do_shortcode()
The function was implemented in version 2.5.0.
Examples
Using Shortcode in a PHP File
This code uses a shortcode in a PHP file, outside the post editor.
echo do_shortcode('[gallery]');
Wrapping Text in Shortcode
In case there’s an opening and closing shortcode, this code wraps text within the shortcode.
echo do_shortcode('[iscorrect]' . $text_to_be_wrapped_in_shortcode . '[/iscorrect]');
Enabling Shortcodes in Text Widgets
This code enables the use of shortcodes in text widgets.
add_filter('widget_text', 'do_shortcode');
Using Shortcodes in a Contact Form
This example demonstrates using shortcodes in a contact form like a Landing Page Template.
echo do_shortcode('[contact-form-7 id="91" title="quote"]');
Storing the Shortcode in a Variable
This is how you can store a shortcode in a variable and then echo it.
$var = do_shortcode('[gallery]'); echo $var;
Parsing Shortcodes in a Custom String
This code makes your custom string parse shortcode.
$string = do_shortcode($string); add_filter('my_string_filter_hook_tag_name', 'do_shortcode');
Using Shortcodes in Single Posts
This code hooks a shortcode into single posts.
add_action('loop_start', 'shortcode_before_entry'); function shortcode_before_entry() { if (!is_singular('post')) { return; } echo do_shortcode('[your_shortcode_handle]'); }
Allowing Specific Shortcodes in Comments
This code only allows specific shortcodes to be used in comments.
add_filter('get_comment_text', function ($comment) { $finalComment = ''; $allowed = ['snippet', 'quote']; $parts = preg_split('/(\[\w+\])/', $comment, null, PREG_SPLIT_DELIM_CAPTURE); for ($i = 0; $i < sizeof($parts); $i++) { if (preg_match('/\[\w+\]/', $parts[$i])) { $shortcodeName = substr($parts[$i], 1, -1); if (in_array($shortcodeName, $allowed)) { $finalComment .= do_shortcode($parts[$i] . $parts[$i+1] . $parts[$i+2]); $i += 2; } else { $finalComment .= $parts[$i]; } } else { $finalComment .= $parts[$i]; } } return $finalComment; });
Defining and Using Shortcodes
This example shows an easy approach to defining and using shortcode.
function wpdocs_custom_shortcode ($content = null) { // Declare Variables $part_1 = $part_2 = $part_3 = $part_4 = $part_5 = ''; // Starting Portion $part_1 .= '<div class="content-wrapper">'; $part_2 .= '<h3 class="content-heading">Here is a heading</h3>'; $part_3 .= '<div class="content-output">'; // Ending Portion $part_4 .= '</div>'; $part_5 .= '</div>'; // Return return $part_1 . $part_2 . $part_3 . do_shortcode($content) . $part_4 . $part_5; } // Register Shortcode add_shortcode('shortcode_name', 'wpdocs_custom_shortcode');
Now you can use [shortcode_name] your content [/shortcode_name]
in your post editor.
Allowing Shortcodes in Comments
This code allows shortcodes in comments.
function shortcodify($comment) { return do_shortcode($comment); } add_filter('comment_text', 'shortcodify'); add_filter('get_comment_text', 'shortcodify');