How to Prevent Duplicate Content on Comment Pages in WordPress

Home » Snippets » How to Prevent Duplicate Content on Comment Pages in WordPress
0

Created with:

Visibility: 

public

Creator: WPTurbo Team

Customize with WPTurbo AI
X

Add Snippet To Project

New Project
Add To Existing Project

If you’re running a WordPress website with a lot of user engagement, you may have noticed that the comments section can get quite long. And if you have paginated comments (where comments are split into multiple pages), you may be concerned about duplicate content. This is because each page of the comments section will have the same content as the previous page, except for a few newer comments. This can negatively impact your SEO, as search engines may think you’re trying to manipulate your rankings with duplicate content. In this article, we’ll explore some solutions to prevent duplicate content on comment pages, and ensure your site remains optimized for search engines.

					function WPTurbo_prevent_duplicate_content_comment_pages( $query ) {
    if ( is_singular() && ( $query->is_main_query() ) ) {
        $post_id = get_queried_object_id();
        if ( isset( $_GET['cpage'] ) && $_GET['cpage'] > 1 ) {
            $page = get_query_var( 'page' ) ? get_query_var( 'page' ) : 1;
            $per_page = get_option( 'comments_per_page' );
            $first_comment = ( ( $page - 1 ) * $per_page ) + 1;
            $last_comment = $first_comment + $per_page - 1;
            $canonical_url = get_permalink( $post_id ) . '#comments';
            if ( $last_comment < get_comments_number( $post_id ) ) {
                $canonical_url = add_query_arg( 'cpage', $_GET['cpage'], $canonical_url );
            }
            wp_redirect( $canonical_url, 301 );
            exit;
        }
    }
}
add_action( 'pre_get_posts', 'WPTurbo_prevent_duplicate_content_comment_pages' );
				

The purpose of this code snippet is to prevent duplicate content issues that may occur when users navigate to different pages of comments on a WordPress post or page.

The first part of the WPTurbo_prevent_duplicate_content_comment_pages() function checks if the current query is for a singular post or page and if it is the main query. This ensures that the code only runs on the post or page single view on the front-end and not for any other queries or views on the site.

Next, we retrieve the queried object ID using the get_queried_object_id() function. This ID is used to construct the canonical URL for the post or page.

If the query string contains a cpage parameter (which is used to determine the page number of the comments), and it is greater than 1, we proceed to create a new URL for the post or page, and redirect the user to it with a 301 status code.

To create the new URL, we get the current page number using the get_query_var() function and page as the parameter, and calculate the first and last comment numbers for the current page. We also retrieve the total number of comments for the post or page using the get_comments_number() function.

Then we use the get_permalink() function to get the permalink for the post or page, and append #comments to it, which jumps the user down to the comments section of the post or page.

If the current page is not the last page of comments, we add the cpage parameter to the URL using the add_query_arg() function, and set it to the value of the cpage parameter in the current query string.

Finally, we use the wp_redirect() function to redirect the user to the new URL we just created, and exit the script using the exit function.

Overall, this code ensures that users are redirected to the correct canonical URL when navigating to different pages of comments on WordPress post or page, avoiding duplicate content issues for search engines.

Register an account to save your snippets or go Pro to get more features.