Make WordPress Core

Opened 2 months ago

Last modified 2 months ago

#62694 new defect (bug)

wpautop Misuse in the "widget_block_content" Filter with Shortcode Block

Reported by: arl1nd's profile arl1nd Owned by:
Milestone: Awaiting Review Priority: normal
Severity: normal Version: 6.7.1
Component: Widgets Keywords: needs-patch
Focuses: Cc:

Description

The widget_block_content filter introduces invalid HTML when processing content from the Shortcode block. Specifically, when a shortcode returns a block-level element like a <div>, it is improperly wrapped in <p> tags, resulting in invalid markup.

Steps to Reproduce

  1. Create a simple shortcode in your theme or plugin:
    <?php
    add_shortcode( 'my_test_shortcode', function() {
        return '<div>something amazing...</div>';
    } );
    
  1. Go to Appearance > Widgets in the WordPress admin area.
  1. Add a Shortcode block and use [my_test_shortcode] as its content.
  1. Save and view the output on the front-end. Notice that the <div> returned by the shortcode is wrapped in <p> tags, resulting in:

    something amazing...

Expected Behavior

The shortcode output should render the block-level <div> without additional wrapping <p> tags, maintaining valid HTML structure.

Proposed Solution

Consider modifying the widget_block_content filter to handle shortcodes correctly and respect block-level elements, preventing improper wrapping by wpautop.

Change History (2)

#1 @dilip2615
2 months ago

Hello @arl1nd you can try this if this suggestion is helpful.

Create a Filter for widget_block_content:
Add this code to your theme's functions.php file or a custom plugin:
If this issue affects multiple shortcodes or needs a global solution, you can add a generalized filter:

<?php
add_filter('widget_block_content', 'handle_shortcode_output', 10, 2);
function handle_shortcode_output($content, $block) {
    remove_filter('the_content', 'wpautop');
    $content = do_shortcode($content);
    add_filter('the_content', 'wpautop');
    return $content;
}

This ensures wpautop won't interfere with any shortcode processed within widget blocks.
Let me know if you need further clarification or adjustments!

#2 @arl1nd
2 months ago

  • Summary changed from Title: wpautop Misuse in the "widget_block_content" Filter with Shortcode Block to wpautop Misuse in the "widget_block_content" Filter with Shortcode Block

Hello @dilip2615 thanks for your proposed solution, although it doesn't solve the problem.

This should be addressed in higher level and patched within the WordPress core itself because it is an actual issue.

The do_blocks(...) function removes the wpautop(...) automatically but... it is added again in wp-includes/blocks/shortcode.php:19

One solution would be removing this wrapper from render_callback but I don't know the real impact of it!

Note: See TracTickets for help on using tickets.