Function and usage of do_shortcode_tag() in wordpress


Answers ( 1 )


    The do_shortcode_tag() function in WordPress is a part of the internal shortcode API. It is primarily a regular expression callable used by the do_shortcode() function, which is the main function for handling shortcodes in WordPress. Here's a detailed explanation and sample usage:


    • Name: do_shortcode_tag
    • Purpose: It's a regular expression callable used within do_shortcode() to parse and handle each shortcode found in the content.
    • Functionality: This function matches and processes a single shortcode found by the regular expression search, calling the corresponding shortcode handler function.


    • Related Function: get_shortcode_regex(). This function generates the regular expression that do_shortcode() uses, and do_shortcode_tag() acts upon the matches found by this regex.
    • Process: When do_shortcode() encounters a shortcode in content, it uses the regex from get_shortcode_regex() to identify and break down the shortcode into various components. These components are then passed to do_shortcode_tag().


    The function do_shortcode_tag() takes one parameter:

    • $m (array, required): This is the array of regular expression matches. The structure is as follows:
      • $m[0] (string): The entire matched shortcode text.
      • $m[1] (string): An optional second opening bracket for escaping shortcodes ([[).
      • $m[2] (string): The shortcode name.
      • $m[3] (string): The shortcode argument list.
      • $m[4] (string): An optional self-closing slash, used for self-closing shortcodes.
      • $m[5] (string): The content of the shortcode when it wraps content.
      • $m[6] (string): An optional second closing bracket for escaping shortcodes (]]).


    • Type: string
    • Description: The output of the shortcode, which is the processed content that replaces the shortcode in the post content.

    Sample Usage

    In typical usage, you wouldn't call do_shortcode_tag() directly. It's used internally by WordPress. However, understanding its role can be important for advanced customization or plugin development. Here's a hypothetical example to illustrate its use:

    function my_shortcode_handler( $atts, $content = null ) {
        // Shortcode logic goes here
        return "<div class='my-shortcode'>" . esc_html($content) . "</div>";
    add_shortcode('my_shortcode', 'my_shortcode_handler');
    // This is a simplified representation of what happens in do_shortcode()
    $content = "Some content [my_shortcode]Hello World![/my_shortcode] more content";
    $pattern = get_shortcode_regex();
    $content = preg_replace_callback( "/$pattern/", 'do_shortcode_tag', $content );

    In this example:

    • We define a shortcode [my_shortcode] which wraps content in a div.
    • do_shortcode() (represented here in a simplified form) uses preg_replace_callback() with do_shortcode_tag() as its callback to process and replace the shortcode in the content.

Leave an answer