do_shortcode()WP 2.5.0

Search content for shortcodes and applies the registered functions to the found shortcodes.

The function will only process known shortcodes (registered shortcodes). Shortcodes are registered using the add_shortcode() function. I.e. the text like [some_name] will not be removed or treated, if the shortcode some_name is not registered.

If the shortcode tag is unknown, the content will be returned as is (the shortcode will not be processed). This can happen when the Plugin is disabled, but its shortcode is used in the content.

Shortcode is a construction in the text that will be processed and replaced with some HTML code. Shortcode can be called in different ways. Some examples of calling different types of shortcodes:

  • [somename]
  • [somename id="123 size="medium"]
  • [somename]text[/somename]
1 time — 0.000387 sec (fast) | 50000 times — 0.71 sec (very fast) | PHP 7.0.32, WP 5.0.3

No Hooks.

Return

String. Content with shortcodes filtered out.

Usage

do_shortcode( $content, $ignore_html );
$content(string) (required)
Content to search for shortcodes.
$ignore_html(true|false)
When true, shortcodes inside HTML elements will be skipped.
Default: false

Examples

0

#1 Process all shortcodes in the text

Code from the WordPress shortcodes.php file. Attaches this function to the_content filter, which is triggered when the post content is displayed:

add_filter( 'the_content', 'do_shortcode', 11 );
0

#2 Processing of a separate shortcode

Example of using a shortcode in a PHP file, outside of content, when you need to get the output of the shortcode separately.

This example shows how to process a separate shortcode in PHP code. For example, if you want to output a shortcode in a template file.

echo do_shortcode('[somename]');

or

echo do_shortcode('[contact-form-7 id="91" title="quote"]');
0

#3 Content shortcode

Adding custom text to the shortcode in a PHP file, outside of the content:

echo do_shortcode( '[iscorrect]'. $text_to_be_wrapped_in_shortcode .'[/iscorrect]' );
0

#4 Enable shortcodes in widget "Text"

if( ! is_admin() ){
	add_filter( 'widget_text', 'do_shortcode' );
}
0

#5 Processing only the specified shortcode in the content

Suppose we want to add support for shortcodes in comments, but we don't want to be able to use all possible shortcodes there. We need only our shortcode to work there, let's call it [myshortcode].

You can implement such a task by leaving only the shortcode you need registered before calling apply_shortcode(). All registered shortcodes are stored in the global variable $shortcode_tags.

// Process the [myshortcode] in the comment content.
// after wpautop
add_filter( 'comment_text', 'do_shortcodes_in_comment_content', 11, 2 );

function do_shortcodes_in_comment_content( $content, $comm ){

	if( 'comment' === $comm->comment_type ){

		$save = $shortcodes = & $GLOBALS['shortcode_tags'];

		$shortcodes = [ 'myshort' => $shortcodes['myshort'] ];

		$content = apply_shortcodes( $content );

		$shortcodes = $save; // return back
	}

	return $content;
}

Notes

  • Global. Array. $shortcode_tags List of shortcode tags and their callback hooks.

Changelog

Since 2.5.0 Introduced.

do_shortcode() code WP 6.4.3

function do_shortcode( $content, $ignore_html = false ) {
	global $shortcode_tags;

	if ( ! str_contains( $content, '[' ) ) {
		return $content;
	}

	if ( empty( $shortcode_tags ) || ! is_array( $shortcode_tags ) ) {
		return $content;
	}

	// Find all registered tag names in $content.
	preg_match_all( '@\[([^<>&/\[\]\x00-\x20=]++)@', $content, $matches );
	$tagnames = array_intersect( array_keys( $shortcode_tags ), $matches[1] );

	if ( empty( $tagnames ) ) {
		return $content;
	}

	// Ensure this context is only added once if shortcodes are nested.
	$has_filter   = has_filter( 'wp_get_attachment_image_context', '_filter_do_shortcode_context' );
	$filter_added = false;

	if ( ! $has_filter ) {
		$filter_added = add_filter( 'wp_get_attachment_image_context', '_filter_do_shortcode_context' );
	}

	$content = do_shortcodes_in_html_tags( $content, $ignore_html, $tagnames );

	$pattern = get_shortcode_regex( $tagnames );
	$content = preg_replace_callback( "/$pattern/", 'do_shortcode_tag', $content );

	// Always restore square braces so we don't break things like <!--[if IE ]>.
	$content = unescape_invalid_shortcodes( $content );

	// Only remove the filter if it was added in this scope.
	if ( $filter_added ) {
		remove_filter( 'wp_get_attachment_image_context', '_filter_do_shortcode_context' );
	}

	return $content;
}