Skip to content

Commit 5bed937

Browse files
kbrown9jeherve
authored andcommitted
Related Posts: Remove the call to remove_filter( 'the_content'… (#13804)
Fixes #13775 The filter 'filter_add_target_to_dom()' removes itself from the 'the_content' hook. Then, a bug in Core causes the filter with the next highest priority to be skipped. For more information on the Core bug see: https://core.trac.wordpress.org/ticket/9968 To avoid this bug, remove the call to "remove_filter( 'the_content', array( $this, 'filter_add_target_to_dom'), ...)". Instead, just rely on conditionals in the 'filter_add_target_to_dom()' method to control when related posts are added to the content. Related posts should not be added to the content under these three condtions: 1. The post contains a Related Posts block. 2. The post contains a 'jetpack-related-posts' shortcode. 3. The 'get_the_excerpt' hook is being executed. This hook is executed when the related posts are being generated, and related posts should not be added to the related post content. These conditions are already handled by the 'filter_add_target_to_dom()' method. However, legacy related posts are currently added to posts that contain a Related Posts block. Fix this by removing the $content parameter from "has_block('jetpack/related-posts', $content)".
1 parent a998fb8 commit 5bed937

File tree

1 file changed

+5
-14
lines changed

1 file changed

+5
-14
lines changed

modules/related-posts/jetpack-related-posts.php

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,8 @@ public function get_headline() {
163163

164164
/**
165165
* Adds a target to the post content to load related posts into if a shortcode for it did not already exist.
166-
* Will skip adding the target if the post content contains a Related Posts block.
166+
* Will skip adding the target if the post content contains a Related Posts block or if the 'get_the_excerpt'
167+
* hook is in the current filter list.
167168
*
168169
* @filter the_content
169170
*
@@ -172,7 +173,7 @@ public function get_headline() {
172173
* @returns string
173174
*/
174175
public function filter_add_target_to_dom( $content ) {
175-
if ( has_block( 'jetpack/related-posts', $content ) ) {
176+
if ( has_block( 'jetpack/related-posts' ) ) {
176177
return $content;
177178
}
178179

@@ -369,17 +370,7 @@ public function render_block( $attributes ) {
369370
'size' => ! empty( $attributes['postsToShow'] ) ? absint( $attributes['postsToShow'] ) : 3,
370371
);
371372

372-
$excludes = $this->parse_numeric_get_arg( 'relatedposts_origin' );
373-
374-
$target_to_dom_priority = has_filter(
375-
'the_content',
376-
array( $this, 'filter_add_target_to_dom' )
377-
);
378-
remove_filter(
379-
'the_content',
380-
array( $this, 'filter_add_target_to_dom' ),
381-
$target_to_dom_priority
382-
);
373+
$excludes = $this->parse_numeric_get_arg( 'relatedposts_origin' );
383374

384375
$related_posts = $this->get_for_post_id(
385376
get_the_ID(),
@@ -452,7 +443,7 @@ public function render_block( $attributes ) {
452443
*
453444
* @uses absint
454445
*
455-
* @param string $arg Name of the GET variable
446+
* @param string $arg Name of the GET variable.
456447
* @return array $result Parsed value(s)
457448
*/
458449
public function parse_numeric_get_arg( $arg ) {

0 commit comments

Comments
 (0)