Function and usage of do_shortcodes_in_html_tags() in wordpress

Question

Answers ( 1 )

    0
    2024-01-06T00:02:59+00:00

    The do_shortcodes_in_html_tags function in WordPress is a specialized function that deals with processing shortcodes within HTML tags in a given content string. Here's a detailed explanation of the function along with its parameters and usage:

    Function:

    do_shortcodes_in_html_tags( string $content, bool $ignore_html, array $tagnames ): string

    Purpose:

    This function is designed to search and process shortcodes specifically inside HTML elements within the provided content. It is particularly useful when you need to handle or filter shortcodes that are embedded within HTML tags, as opposed to those that are simply part of the text content.

    Description:

    • The function scans through the provided $content for shortcodes, but its focus is restricted to those shortcodes that exist inside HTML elements.
    • If there are any [ or ] characters left inside elements after processing, the function will HTML encode them ([ for [ and ] for ]). This encoding is crucial to prevent any interference with shortcodes that are outside the HTML elements.
    • It is assumed that the $content has already been processed by the KSES system in WordPress, which is a mechanism for filtering out harmful HTML.
    • Users with the unfiltered_html capability in WordPress should be cautious. If angle braces are nested in tags in their content, they might see unexpected outputs due to the way this function processes the content.

    Parameters:

    1. $content (string - required):

      • This is the content within which the function will search for shortcodes. It should be a string that may contain HTML tags and shortcodes.
    2. $ignore_html (bool - required):

      • This parameter dictates how the function handles square braces inside HTML elements. When set to true, all square braces ([ and ]) inside elements will be HTML encoded. This is important to avoid conflicts with other shortcodes.
    3. $tagnames (array - required):

      • This is an array of shortcode tags that the function should look for within the HTML elements of the $content. Only the shortcodes listed in this array will be considered for processing.

    Return:

    • String: The function returns the modified content as a string. This content will have the specified shortcodes processed and any remaining square braces inside HTML elements encoded.

    Sample Usage:

    $content = '<div>[shortcode1] Example content [shortcode2]</div>';
    $ignore_html = true;
    $tagnames = ['shortcode1', 'shortcode2'];
    
    $processed_content = do_shortcodes_in_html_tags($content, $ignore_html, $tagnames);
    
    echo $processed_content;
    

    In this example, do_shortcodes_in_html_tags will process shortcode1 and shortcode2 found inside the <div> tag in the $content. The $ignore_html parameter being true means any [ or ] inside the HTML tags will be encoded to avoid conflicts.

Leave an answer