WordPress at a glance

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.000387s = fast | 50000 times = 0.71s = 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

#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 );

#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"]');

#3. Custom text in a separate 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]');

#4. Enable shortcodes in widget "Text"

if( ! is_admin() )
	add_filter( 'widget_text', 'do_shortcode' );

Notes

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

Changelog

Since 2.5.0 Introduced.

Code of do shortcode: wp-includes/shortcodes.php WP 5.2.4

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

	if ( false === strpos( $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;
	}

	$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 );

	return $content;
}

Related Functions

From category: Shortcodes

No comments
    Hello, !     Log In . Register