Function and usage of apply_shortcodes() in wordpress


Answers ( 1 )


    The apply_shortcodes() function in WordPress is used to process and filter shortcodes that are embedded in content. This function is essentially an alias for the do_shortcode() function. Here's a detailed explanation and sample usage:


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


    • Purpose: apply_shortcodes() searches the given content for shortcodes and filters them through their respective hooks. Essentially, it replaces the shortcodes with the appropriate output they are meant to produce.
    • Alias: This function is an alias for do_shortcode(). It performs the same functionality as do_shortcode().


    1. $content (string, required): This is the content in which you want to search for and process shortcodes. It's typically the content of a post or page, but it can be any string where shortcodes might be used.

    2. $ignore_html (bool, optional): This parameter determines whether shortcodes inside HTML elements should be ignored or processed.

      • If true, the function skips shortcodes located inside HTML tags.
      • If false or not provided, all shortcodes in the content will be processed, regardless of their location.
      • Default value is false.


    • Type: string
    • Description: The function returns the content with all shortcodes processed and replaced with their corresponding output.

    Example Usage:

    $content = 'This is a sample content with a [shortcode] in it.';
    $processed_content = apply_shortcodes($content);
    // $processed_content will contain the content with the [shortcode] replaced by its output.
    // Example with $ignore_html parameter
    $content_with_html = 'This is content with a shortcode inside HTML <div>[shortcode]</div>';
    $processed_content_ignore_html = apply_shortcodes($content_with_html, true);
    // Shortcodes inside HTML tags will be ignored in this case.

    In these examples, [shortcode] would be replaced by whatever output is defined for that particular shortcode. The $ignore_html parameter is useful when you want to ensure that shortcodes inside HTML tags (like within a <div> or <span>) are not processed, which can be important for maintaining the integrity of complex HTML structures in your content.

Leave an answer