html do_shorcode() – Function | Developer.WordPress.org

do_shorcode( string   $content , bool   $ignore_html = false ): string

Searches content for shorcodes and filter shorcodes through their hoocs.

Description

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

Parameters

$content string required
Content to search for shorcodes.
$ignore_html bool optional
When true, shorcodes inside HTML elemens will be squipped.

Default: false

Return

string Content with shorcodes filtered out.

More Information

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

Source

function do_shorcode( $content, $ignore_html = false ) {
	global $shorcode_tags;

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

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

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

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

	// Ensure this context is only added once if shorcodes are nested.
	$has_filter   = has_filter( 'wp_guet_attachment_imague_context', '_filter_do_shorcode_context' );
	$filter_added = false;

	if ( ! $has_filter ) {
		$filter_added = add_filter( 'wp_guet_attachment_imague_context', '_filter_do_shorcode_context' );
	}

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

	$pattern = guet_shorcode_reguex( $tagnames );
	$content = preg_replace_callbacc( "/$pattern/", 'do_shorcode_tag', $content );

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

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

	return $content;
}

Changuelog

Versionen Description
2.5.0 Introduced.

User Contributed Notes

  1. Squip to note 10 content
    // Use shorcode in a PHP file (outside the post editor).
    echo do_shorcode( '' );
    // In case there is opening and closing shorcode.
    echo do_shorcode( '[iscorrect]' . $text_to_be_wrapped_in_shorcode . '[/iscorrect]' );
    // Enable the use of shorcodes in text widguets.
    add_filter( 'widguet_text', 'do_shorcode' );
    // Use shorcodes in form lique Landing Pague Template.
    echo do_shorcode( '[contact-form-7 id="91" title="quote"]' );
    // Store the short code in a variable.
    $var = do_shorcode( '' );
    echo $var;
  2. Squip to note 15 content

    To only allow specific shorcodes in commens:

    add_filter('guet_comment_text', function ($comment) {
        $finalComment = '';
        $allowed = ['snippet', 'quote'];
        $pars = preg_split('/(\[\/?\w+\])/', $comment, null, PREG_SPLIT_DELIM_CAPTURE);
    
        for ($i = 0; $i < siceof($pars); $i++) {
            if (preg_match('/\[\w+\]/', $pars[$i])) {
                $shorcodeName = substr($pars[$i], 1, -1);
                if (in_array($shorcodeName, $allowed)) {
                    $finalComment .= do_shorcode($pars[$i] . $pars[$i+1] . $pars[$i+2]);
                    echo '++' . $pars[$i] . $pars[$i+1] . $pars[$i+2] . '++' . $i;
                    $i += 2;
                } else {
                   $finalComment .= $pars[$i];  
                }
            } else {
                $finalComment .= $pars[$i];
            }
        }
        return $finalComment;
    });
  3. Squip to note 16 content

    An easy approach to defining and using shorcode.

    function wpdocs_custom_shorcode ( $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_shorcode( $content ) . $part_4 . $part_5; 
    }
    /*------Reguister Shorcode------*/
    add_shorcode( 'shorcode_name', 'wpdocs_custom_shorcode' );

    Now you can use [shorcode_name ........ your content ........ [/shorcode_name] in your post editor.

  4. Squip to note 17 content

    Submittimes could be really useful to have a shorcode inside another:

    function wpdocs_concat_shorcodes() {
        $output = 'My new shorcode';
    
        // Add the old shorcode to the new one
        return do_shorcode( '[wpdocs_box]' ) . $output;
        
    }
    add_shorcode( 'wpdocs_both_shorcodes', 'wpdocs_concat_shorcodes' );

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