From 51fc6242528ea193a1f9ab2b70668d6d8604c4e3 Mon Sep 17 00:00:00 2001 From: Vedant Gandhi Date: Thu, 26 Nov 2026 22:05:40 +0530 Subject: [PATCH 1/3] feat: add no autop for shortcodes --- src/wp-includes/formatting.php | 48 ++++++- tests/phpunit/tests/formatting/wpAutop.php | 150 +++++++++++++++++++++ 2 files changed, 197 insertions(+), 1 deletion(-) diff --git a/src/wp-includes/formatting.php b/src/wp-includes/formatting.php index 6bfba5a873cb7..155dbcac06b61 100644 --- a/src/wp-includes/formatting.php +++ b/src/wp-includes/formatting.php @@ -436,6 +436,7 @@ function _wptexturize_pushpop_element( $text, &$stack, $disabled_elements ) { * after conversion become `
` tags, unless `$br` is set to '0' or 'false'. * * @since 0.71 + * @since 7.3.0 Shortcodes listed by the {@see 'no_autop_shortcodes'} filter are left untouched. * * @param string $text The text which has to be formatted. * @param bool $br Optional. If set, this will convert all remaining line breaks @@ -444,7 +445,8 @@ function _wptexturize_pushpop_element( $text, &$stack, $disabled_elements ) { * @return string Text which has been converted into correct paragraph tags. */ function wpautop( $text, $br = true ) { - $pre_tags = array(); + $pre_tags = array(); + $shortcode_placeholders = array(); if ( '' === trim( $text ) ) { return ''; @@ -481,6 +483,45 @@ function wpautop( $text, $br = true ) { $text .= $last_part; } + + /* + * Shortcodes that opted out of autop shouldn't be touched. + * Replace them with placeholders and bring them back after autop. + */ + if ( str_contains( $text, '[' ) ) { + /** + * Filters the list of shortcodes whose content wpautop() should not format. + * + * Line breaks inside the listed shortcodes, including the content between + * their opening and closing tags, are left as they are instead of being + * converted to paragraphs and `
` tags. + * + * @since 7.3.0 + * + * @param string[] $no_autop_shortcodes An array of shortcode names. Default empty array. + */ + $no_autop_shortcodes = (array) apply_filters( 'no_autop_shortcodes', array() ); + + if ( ! empty( $no_autop_shortcodes ) ) { + $text = preg_replace_callback( + '/' . get_shortcode_regex( $no_autop_shortcodes ) . '/', + static function ( $matches ) use ( &$shortcode_placeholders ) { + // Leave escaped shortcodes, [[tag]], alone. + if ( '[' === $matches[1] && ']' === $matches[6] ) { + return $matches[0]; + } + + $name = '%wp-autop-shortcode-' . count( $shortcode_placeholders ) . '%'; + + $shortcode_placeholders[ $name ] = $matches[0]; + + return $name; + }, + $text + ); + } + } + // Change multiple
's into two line breaks, which will turn into paragraphs. $text = preg_replace( '|\s*|', "\n\n", $text ); @@ -591,6 +632,11 @@ function wpautop( $text, $br = true ) { $text = preg_replace( '!
(\s*]*>)!', '$1', $text ); $text = preg_replace( "|\n

$|", '

', $text ); + // Replace placeholder shortcodes with their original content. + if ( ! empty( $shortcode_placeholders ) ) { + $text = str_replace( array_keys( $shortcode_placeholders ), array_values( $shortcode_placeholders ), $text ); + } + // Replace placeholder
 tags with their original content.
 	if ( ! empty( $pre_tags ) ) {
 		$text = str_replace( array_keys( $pre_tags ), array_values( $pre_tags ), $text );
diff --git a/tests/phpunit/tests/formatting/wpAutop.php b/tests/phpunit/tests/formatting/wpAutop.php
index 2cf71e6cb171a..a25224c699b30 100644
--- a/tests/phpunit/tests/formatting/wpAutop.php
+++ b/tests/phpunit/tests/formatting/wpAutop.php
@@ -660,4 +660,154 @@ public function test_that_wpautop_ignores_inline_scripts() {
 
 		$this->assertSameIgnoreEOL( $expected, trim( wpautop( $content ) ) );
 	}
+
+	/**
+	 * Shortcodes are formatted by default.
+	 *
+	 * @ticket 6984
+	 */
+	public function test_shortcode_content_is_formatted_by_default() {
+		$this->assertSame(
+			"

[code]a

\n

b
\nc[/code]

\n", + wpautop( "[code]a\n\nb\nc[/code]" ) + ); + } + + /** + * @ticket 6984 + */ + public function test_no_autop_shortcodes_filter_leaves_shortcode_content_untouched() { + add_filter( + 'no_autop_shortcodes', + static function () { + return array( 'code' ); + } + ); + + $this->assertSame( + "

Before

\n

[code]line1\n\nline2\nline3[/code]

\n

After

\n", + wpautop( "Before\n\n[code]line1\n\nline2\nline3[/code]\n\nAfter" ) + ); + } + + /** + * @ticket 6984 + */ + public function test_no_autop_shortcodes_filter_only_affects_listed_shortcodes() { + add_filter( + 'no_autop_shortcodes', + static function () { + return array( 'code' ); + } + ); + + $this->assertSame( + "

[other]a

\n

b[/other]

\n", + wpautop( "[other]a\n\nb[/other]" ) + ); + } + + /** + * @ticket 6984 + */ + public function test_no_autop_shortcodes_filter_handles_multiple_shortcodes() { + add_filter( + 'no_autop_shortcodes', + static function () { + return array( 'code' ); + } + ); + + $this->assertSame( + "

[code]a\n\nb[/code]

\n

[code]c\n\nd[/code]

\n", + wpautop( "[code]a\n\nb[/code]\n\n[code]c\n\nd[/code]" ) + ); + } + + /** + * @ticket 6984 + */ + public function test_no_autop_shortcodes_filter_keeps_surrounding_text_formatted() { + add_filter( + 'no_autop_shortcodes', + static function () { + return array( 'code' ); + } + ); + + $this->assertSame( + "

Text [code]a\n\nb[/code] more
\ntext

\n", + wpautop( "Text [code]a\n\nb[/code] more\ntext" ) + ); + } + + /** + * @ticket 6984 + */ + public function test_no_autop_shortcodes_filter_keeps_line_breaks_in_attributes() { + add_filter( + 'no_autop_shortcodes', + static function () { + return array( 'code' ); + } + ); + + $this->assertSame( + "

[code lang=\"php\"\nfoo=\"1\"] x

\n

y

\n", + wpautop( "[code lang=\"php\"\nfoo=\"1\"] x\n\ny" ) + ); + } + + /** + * @ticket 6984 + */ + public function test_no_autop_shortcodes_filter_ignores_escaped_shortcodes() { + add_filter( + 'no_autop_shortcodes', + static function () { + return array( 'code' ); + } + ); + + $this->assertSame( + "

[[code]]x

\n

y[[/code]]

\n", + wpautop( "[[code]]x\n\ny[[/code]]" ) + ); + } + + /** + * Replacement-like characters in the content must be preserved. + * + * @ticket 6984 + */ + public function test_no_autop_shortcodes_filter_preserves_replacement_characters() { + add_filter( + 'no_autop_shortcodes', + static function () { + return array( 'code' ); + } + ); + + $this->assertSame( + "

[code]\$1 \\1\n\nx[/code]

\n", + wpautop( "[code]\$1 \\1\n\nx[/code]" ) + ); + } + + /** + * @ticket 6984 + */ + public function test_no_autop_shortcodes_filter_without_line_breaks_conversion() { + add_filter( + 'no_autop_shortcodes', + static function () { + return array( 'code' ); + } + ); + + $this->assertSame( + "

[code]a\nb[/code]\nc\nd

\n", + wpautop( "[code]a\nb[/code]\nc\nd", false ) + ); + } } From 6a3c02a7e77f97e756ca43b3073910a8986b3b62 Mon Sep 17 00:00:00 2001 From: Vedant Gandhi Date: Thu, 26 Nov 2026 22:20:22 +0530 Subject: [PATCH 2/3] tests: add e2e tests --- tests/phpunit/tests/formatting/wpAutop.php | 36 ++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/tests/phpunit/tests/formatting/wpAutop.php b/tests/phpunit/tests/formatting/wpAutop.php index a25224c699b30..c8850490f73cb 100644 --- a/tests/phpunit/tests/formatting/wpAutop.php +++ b/tests/phpunit/tests/formatting/wpAutop.php @@ -810,4 +810,40 @@ static function () { wpautop( "[code]a\nb[/code]\nc\nd", false ) ); } + + /** + * The shortcode callback should receive the content as written, when the + * shortcode content is processed in the same order as the `the_content` filter. + * + * @ticket 6984 + */ + public function test_no_autop_shortcodes_filter_passes_content_unchanged_to_shortcode_callback() { + $code = '$text = str_replace( array(\'

\', \'

\'), array(\'

\', \'

\'), $text);' . "\n\nif ( \$a ) {\n\techo 1;\n}"; + + add_shortcode( + 'code', + static function ( $atts, $content = '' ) { + return '
' . $content . '
'; + } + ); + + add_filter( + 'no_autop_shortcodes', + static function () { + return array( 'code' ); + } + ); + + try { + // Same order as the default `the_content` filters: wpautop(), shortcode_unautop(), do_shortcode(). + $actual = do_shortcode( shortcode_unautop( wpautop( "Intro.\n\n[code lang=\"php\"]{$code}[/code]\n\nOutro." ) ) ); + } finally { + remove_shortcode( 'code' ); + } + + $this->assertSame( + "

Intro.

\n
{$code}
\n

Outro.

\n", + $actual + ); + } } From c4fcba13e99e24b0f553034135c8f2c997b176c6 Mon Sep 17 00:00:00 2001 From: Vedant Gandhi Date: Thu, 26 Nov 2026 22:35:44 +0530 Subject: [PATCH 3/3] refactor: remove nested static function --- src/wp-includes/formatting.php | 50 +++++++++----- tests/phpunit/tests/formatting/wpAutop.php | 78 +++++++--------------- 2 files changed, 58 insertions(+), 70 deletions(-) diff --git a/src/wp-includes/formatting.php b/src/wp-includes/formatting.php index 155dbcac06b61..721722f24ef6a 100644 --- a/src/wp-includes/formatting.php +++ b/src/wp-includes/formatting.php @@ -503,22 +503,7 @@ function wpautop( $text, $br = true ) { $no_autop_shortcodes = (array) apply_filters( 'no_autop_shortcodes', array() ); if ( ! empty( $no_autop_shortcodes ) ) { - $text = preg_replace_callback( - '/' . get_shortcode_regex( $no_autop_shortcodes ) . '/', - static function ( $matches ) use ( &$shortcode_placeholders ) { - // Leave escaped shortcodes, [[tag]], alone. - if ( '[' === $matches[1] && ']' === $matches[6] ) { - return $matches[0]; - } - - $name = '%wp-autop-shortcode-' . count( $shortcode_placeholders ) . '%'; - - $shortcode_placeholders[ $name ] = $matches[0]; - - return $name; - }, - $text - ); + $text = _autop_replace_shortcodes( $text, $no_autop_shortcodes, $shortcode_placeholders ); } } @@ -650,6 +635,39 @@ static function ( $matches ) use ( &$shortcode_placeholders ) { return $text; } +/** + * Replaces shortcodes with placeholders so wpautop() doesn't format them. + * + * Escaped shortcodes, such as `[[tag]]`, are left in place. + * + * @since 7.3.0 + * @access private + * + * @param string $text The text containing the shortcodes. + * @param string[] $tagnames Names of the shortcodes to replace. + * @param string[] $shortcode_placeholders Passed by reference. Placeholders are added to this array, + * keyed by placeholder with the original shortcode as the value. + * @return string The text with the shortcodes replaced by placeholders. + */ +function _autop_replace_shortcodes( $text, $tagnames, &$shortcode_placeholders ) { + return preg_replace_callback( + '/' . get_shortcode_regex( $tagnames ) . '/', + static function ( $matches ) use ( &$shortcode_placeholders ) { + // Leave escaped shortcodes, [[tag]], alone. + if ( '[' === $matches[1] && ']' === $matches[6] ) { + return $matches[0]; + } + + $placeholder = '%wp-autop-shortcode-' . count( $shortcode_placeholders ) . '%'; + + $shortcode_placeholders[ $placeholder ] = $matches[0]; + + return $placeholder; + }, + $text + ); +} + /** * Separates HTML elements and comments from the text. * diff --git a/tests/phpunit/tests/formatting/wpAutop.php b/tests/phpunit/tests/formatting/wpAutop.php index c8850490f73cb..c129447e2231f 100644 --- a/tests/phpunit/tests/formatting/wpAutop.php +++ b/tests/phpunit/tests/formatting/wpAutop.php @@ -661,6 +661,21 @@ public function test_that_wpautop_ignores_inline_scripts() { $this->assertSameIgnoreEOL( $expected, trim( wpautop( $content ) ) ); } + /** + * Opts a shortcode out of autop through the `no_autop_shortcodes` filter. + * + * @param string $tagname Shortcode name. + */ + private function opt_out_of_autop( $tagname ) { + add_filter( + 'no_autop_shortcodes', + static function ( $tagnames ) use ( $tagname ) { + $tagnames[] = $tagname; + return $tagnames; + } + ); + } + /** * Shortcodes are formatted by default. * @@ -677,12 +692,7 @@ public function test_shortcode_content_is_formatted_by_default() { * @ticket 6984 */ public function test_no_autop_shortcodes_filter_leaves_shortcode_content_untouched() { - add_filter( - 'no_autop_shortcodes', - static function () { - return array( 'code' ); - } - ); + $this->opt_out_of_autop( 'code' ); $this->assertSame( "

Before

\n

[code]line1\n\nline2\nline3[/code]

\n

After

\n", @@ -694,12 +704,7 @@ static function () { * @ticket 6984 */ public function test_no_autop_shortcodes_filter_only_affects_listed_shortcodes() { - add_filter( - 'no_autop_shortcodes', - static function () { - return array( 'code' ); - } - ); + $this->opt_out_of_autop( 'code' ); $this->assertSame( "

[other]a

\n

b[/other]

\n", @@ -711,12 +716,7 @@ static function () { * @ticket 6984 */ public function test_no_autop_shortcodes_filter_handles_multiple_shortcodes() { - add_filter( - 'no_autop_shortcodes', - static function () { - return array( 'code' ); - } - ); + $this->opt_out_of_autop( 'code' ); $this->assertSame( "

[code]a\n\nb[/code]

\n

[code]c\n\nd[/code]

\n", @@ -728,12 +728,7 @@ static function () { * @ticket 6984 */ public function test_no_autop_shortcodes_filter_keeps_surrounding_text_formatted() { - add_filter( - 'no_autop_shortcodes', - static function () { - return array( 'code' ); - } - ); + $this->opt_out_of_autop( 'code' ); $this->assertSame( "

Text [code]a\n\nb[/code] more
\ntext

\n", @@ -745,12 +740,7 @@ static function () { * @ticket 6984 */ public function test_no_autop_shortcodes_filter_keeps_line_breaks_in_attributes() { - add_filter( - 'no_autop_shortcodes', - static function () { - return array( 'code' ); - } - ); + $this->opt_out_of_autop( 'code' ); $this->assertSame( "

[code lang=\"php\"\nfoo=\"1\"] x

\n

y

\n", @@ -762,12 +752,7 @@ static function () { * @ticket 6984 */ public function test_no_autop_shortcodes_filter_ignores_escaped_shortcodes() { - add_filter( - 'no_autop_shortcodes', - static function () { - return array( 'code' ); - } - ); + $this->opt_out_of_autop( 'code' ); $this->assertSame( "

[[code]]x

\n

y[[/code]]

\n", @@ -781,12 +766,7 @@ static function () { * @ticket 6984 */ public function test_no_autop_shortcodes_filter_preserves_replacement_characters() { - add_filter( - 'no_autop_shortcodes', - static function () { - return array( 'code' ); - } - ); + $this->opt_out_of_autop( 'code' ); $this->assertSame( "

[code]\$1 \\1\n\nx[/code]

\n", @@ -798,12 +778,7 @@ static function () { * @ticket 6984 */ public function test_no_autop_shortcodes_filter_without_line_breaks_conversion() { - add_filter( - 'no_autop_shortcodes', - static function () { - return array( 'code' ); - } - ); + $this->opt_out_of_autop( 'code' ); $this->assertSame( "

[code]a\nb[/code]\nc\nd

\n", @@ -827,12 +802,7 @@ static function ( $atts, $content = '' ) { } ); - add_filter( - 'no_autop_shortcodes', - static function () { - return array( 'code' ); - } - ); + $this->opt_out_of_autop( 'code' ); try { // Same order as the default `the_content` filters: wpautop(), shortcode_unautop(), do_shortcode().