diff --git a/src/wp-includes/formatting.php b/src/wp-includes/formatting.php index 462ebaf1f4..dc4bf24648 100644 --- a/src/wp-includes/formatting.php +++ b/src/wp-includes/formatting.php @@ -3065,6 +3065,11 @@ function wp_targeted_link_rel_callback( $matches ) { */ $rel = apply_filters( 'wp_targeted_link_rel', 'noopener noreferrer', $link_html ); + // Avoid additional regex if the filter removes rel values. + if ( ! $rel ) { + return ""; + } + // Value with delimiters, spaces around are optional. $attr_regex = '|rel\s*=\s*?(\\\\{0,1}["\'])(.*?)\\1|i'; preg_match( $attr_regex, $link_html, $rel_match ); diff --git a/tests/phpunit/tests/formatting/WPTargetedLinkRel.php b/tests/phpunit/tests/formatting/WPTargetedLinkRel.php index 49c843ec2c..5693f02973 100644 --- a/tests/phpunit/tests/formatting/WPTargetedLinkRel.php +++ b/tests/phpunit/tests/formatting/WPTargetedLinkRel.php @@ -71,4 +71,16 @@ class Tests_Targeted_Link_Rel extends WP_UnitTestCase { $expected = '

Links: Change me Do not change me

'; $this->assertEquals( $expected, wp_targeted_link_rel( $content ) ); } + + /** + * Ensure empty rel attributes are not added. + * + * @ticket 45352. + */ + public function test_ignore_if_wp_targeted_link_rel_nulled() { + add_filter( 'wp_targeted_link_rel', '__return_empty_string' ); + $content = '

Links: Do not change me

'; + $expected = '

Links: Do not change me

'; + $this->assertEquals( $expected, wp_targeted_link_rel( $content ) ); + } }