diff --git a/src/wp-includes/formatting.php b/src/wp-includes/formatting.php
index 6bfba5a873cb7..721722f24ef6a 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,30 @@ 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 = _autop_replace_shortcodes( $text, $no_autop_shortcodes, $shortcode_placeholders );
+ }
+ }
+
// Change multiple
's into two line breaks, which will turn into paragraphs.
$text = preg_replace( '|
\s*
|', "\n\n", $text );
@@ -591,6 +617,11 @@ function wpautop( $text, $br = true ) {
$text = preg_replace( '!
(\s*?(?:p|li|div|dl|dd|dt|th|pre|td|ul|ol)[^>]*>)!', '$1', $text );
$text = preg_replace( "|\n
tags with their original content.
if ( ! empty( $pre_tags ) ) {
$text = str_replace( array_keys( $pre_tags ), array_values( $pre_tags ), $text );
@@ -604,6 +635,39 @@ function wpautop( $text, $br = true ) {
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 2cf71e6cb171a..c129447e2231f 100644
--- a/tests/phpunit/tests/formatting/wpAutop.php
+++ b/tests/phpunit/tests/formatting/wpAutop.php
@@ -660,4 +660,160 @@ 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.
+ *
+ * @ticket 6984
+ */
+ public function test_shortcode_content_is_formatted_by_default() {
+ $this->assertSame(
+ "[code]a
\nb
\nc[/code]
\n",
+ wpautop( "[code]a\n\nb\nc[/code]" )
+ );
+ }
+
+ /**
+ * @ticket 6984
+ */
+ public function test_no_autop_shortcodes_filter_leaves_shortcode_content_untouched() {
+ $this->opt_out_of_autop( 'code' );
+
+ $this->assertSame(
+ "Before
\n[code]line1\n\nline2\nline3[/code]
\nAfter
\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() {
+ $this->opt_out_of_autop( 'code' );
+
+ $this->assertSame(
+ "[other]a
\nb[/other]
\n",
+ wpautop( "[other]a\n\nb[/other]" )
+ );
+ }
+
+ /**
+ * @ticket 6984
+ */
+ public function test_no_autop_shortcodes_filter_handles_multiple_shortcodes() {
+ $this->opt_out_of_autop( '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() {
+ $this->opt_out_of_autop( '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() {
+ $this->opt_out_of_autop( 'code' );
+
+ $this->assertSame(
+ "[code lang=\"php\"\nfoo=\"1\"] x
\ny
\n",
+ wpautop( "[code lang=\"php\"\nfoo=\"1\"] x\n\ny" )
+ );
+ }
+
+ /**
+ * @ticket 6984
+ */
+ public function test_no_autop_shortcodes_filter_ignores_escaped_shortcodes() {
+ $this->opt_out_of_autop( 'code' );
+
+ $this->assertSame(
+ "[[code]]x
\ny[[/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() {
+ $this->opt_out_of_autop( '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() {
+ $this->opt_out_of_autop( 'code' );
+
+ $this->assertSame(
+ "[code]a\nb[/code]\nc\nd
\n",
+ 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 . '
';
+ }
+ );
+
+ $this->opt_out_of_autop( '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}\nOutro.
\n",
+ $actual
+ );
+ }
}