do_shortcode ( string $content, bool $ignore_html = false )

Search content for shortcodes and filter shortcodes through their hooks.

If there are no shortcode tags defined, then the content will be returned without any filtering. This might cause issues when plugins are disabled but the shortcode will still show up in the post or content.

Return: (string) Content with shortcodes filtered out.

Source file: wp-includes/shortcodes.php

View source


Parameters

$content

(string) (Required) Content to search for shortcodes.

$ignore_html

(bool) (Optional) When true, shortcodes inside HTML elements will be skipped.

Default value: false


Explanation

If there are no shortcode tags defined, then the content will be returned without any filtering. This might cause issues if a plugin is disabled as its shortcode will still show up in the post or content.



Source

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;

	$tagnames = array_keys($shortcode_tags);
	$tagregexp = join( '|', array_map('preg_quote', $tagnames) );
	$pattern = "/\\[($tagregexp)/s";

	if ( 1 !== preg_match( $pattern, $content ) ) {
		// Avoids parsing HTML when there are no shortcodes or embeds anyway.
		return $content;
	}

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

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

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

	return $content;
}


Changelog


User Contributed Notes

  1. add_filter( 'the_content', 'do_shortcode', 11 ); // From shortcodes.php
    
    // Use shortcode in a PHP file (outside the post editor).
    echo do_shortcode( '' );
    
    // In case there is opening and closing shortcode.
    echo do_shortcode( '[iscorrect]' . $text_to_be_wrapped_in_shortcode . '[/iscorrect]' );
    
    // Enable the use of shortcodes in text widgets.
    add_filter( 'widget_text', 'do_shortcode' );
    
    // Use shortcodes in form like Landing Page Template.
    echo do_shortcode( '[contact-form-7 id="91" title="quote"]' );
    
    // Store the short code in a variable.
    $var = do_shortcode( '' );
    echo $var;
    

    There is an exception to the built-in embed shortcode available with WordPress. In order to use this shortcode with do_shortcode, you can do the following:

    $embedurl = 'http://yourembeddableurl.com';
    if ( ! empty( $embedurl ) ) {
    	$var = apply_filters( 'the_content', "" );
    	echo $var;
    }
    

You must log in before being able to contribute a note.