Is shortcode_unautop() broken?

shortcode_unautop() in /wp-includes/formatting.php is supposed to find shortcodes in a block of text and remove wrapping paragraph tags from them.

I’ve been having issues with paragraph tags making it through this process.

Here’s the output from var_dump($pee), which I placed at the very beginning of the function, i.e., the string before being processed:

string(353) "<p>[row wrap="true"]</p>
<p>[one_half]</p>
<p>[text_block]Fusce blandit adipiscing libero, nec bibendum diam volutpat tempor.[/text_block]</p>
<p>[/one_half]</p>
<p>[one_half last="true"]</p>
<p>[text_block]Donec fermentum diam leo, ut convallis nisl tristique ut. Ut rhoncus leo vitae tempus pulvinar.[/text_block]</p>
<p>[/one_half]</p>
<p>[/row]</p>
"

All wrapped in paragraph tags, as expected.

I then put var_dump(preg_replace( $pattern, '$1', $pee )); just before the end of the function, which gives:

string(346) "[row wrap="true"]</p>
<p>[one_half]</p>
<p>[text_block]Fusce blandit adipiscing libero, nec bibendum diam volutpat tempor.[/text_block]</p>
<p>[/one_half]</p>
<p>[one_half last="true"]</p>
<p>[text_block]Donec fermentum diam leo, ut convallis nisl tristique ut. Ut rhoncus leo vitae tempus pulvinar.[/text_block]</p>
<p>[/one_half]</p>
<p>[/row]
"

All it has done is remove the opening and closing tags from the entire block, instead of each individual shortcode. I checked the value of global $shortcode_tags; and all the shortcodes in my example were in it.

Is the function broken, or am I expecting too much from it? I’m pretty sure I’m not and it is supposed to remove all the paragraph tags – but I can’t help thinking something else is going on, like unexpected space characters or something.

Solutions Collecting From Web of "Is shortcode_unautop() broken?"

It seems the function is broken. The issue is in trac: https://core.trac.wordpress.org/ticket/14050

I am using this to solve the problem temporarily: https://core.trac.wordpress.org/attachment/ticket/14050/plugin.php . This code still fails some of the tests used, but it completely resolves the issue I described, so I will use it unless I find something practical that it breaks.

I’ve been reading the WordPress documentation about this function and the behaviour you are having there, is the one expected to happen.

This function clean outside of the shortcode and not inside. The regex is not checking the content of the shortcode.

EDIT:

https://developer.wordpress.org/reference/functions/shortcode_unautop/

Don’t auto-p wrap shortcodes that stand alone

Ensures that shortcodes are not wrapped in <p>...</p>.

Everything is said there.