Separate Page for Post Comments
The code below designed as a plugin and creates a separate comments page for single post of the site. A new page .../comments/ appears for each post, it displays comments of the post.
Let's do the stuff - create a normal WordPress plugin.
- Create a plugin folder comments-page.
- Create inside the file
comments-page.php. - Activate the plugin. OR You can add the code in theme's
function.php.
<?php
/**
* Plugin Name: Kama Post Comments Single Page
* Description: Creates single page for post comments.
* Version: 1.0
* Author: Kama
* Author URI: http://wp-kama.com
*/
$Kama_Separate_Comments_Page = new Kama_Separate_Comments_Page;
$Kama_Separate_Comments_Page->init();
class Kama_Separate_Comments_Page {
static $page_title_patt = "Comments for %s";
function init(){
add_filter( 'query_vars', [ $this, 'query_vars' ] );
add_action( 'init', [ $this, 'add_endpoint' ] );
add_action( 'single_template', [ $this, 'template_redirect' ] );
add_filter( 'get_comment_link', [ $this, 'get_comment_link' ] );
add_filter( 'wp_title', [ $this, 'wp_title' ], 10, 1 );
register_activation_hook( __FILE__, [ $this, 'activate'] );
register_deactivation_hook( __FILE__, [ $this, 'deactivate'] );
}
function query_vars( $vars ){
$vars[] = 'comments';
return $vars;
}
# Add a /comments/ page to all post permalinks
function add_endpoint(){
add_rewrite_endpoint( 'comments', EP_PERMALINK );
}
# Template file for the /comments/ permalink
function template_redirect( $templates = '' ){
global $wp_query;
if( ! isset( $wp_query->query['comments'] ) )
return $templates;
$templates = locate_template( 'comments-page-template.php', false );
if( ! $templates ){
$templates = __DIR__ . '/comments-page-template.php';
}
return $templates;
}
# Fix comment permalinks
function get_comment_link( $url ){
$urlparts = explode( '#', $url );
return untrailingslashit( $urlparts[0] ) . '/comments/#' . $urlparts[1];
}
# Fix the page title
function wp_title( $title ){
global $wp_query;
if( isset( $wp_query->query['comments'] ) )
$title = sprintf( self::$page_title_patt, $title );
return $title;
}
function activate(){
$this->add_endpoint();
flush_rewrite_rules();
}
function deactivate(){
flush_rewrite_rules();
}
}
Now you need to create a template file. It will be responsible for the output of our page with pos tcomments. The file should be named comments-page-template.php and should be placd in the plugin folder or in the current theme folder (child or parent).
Fix code below for your theme.
<?php get_header(); ?> <article id="post" class="hentry <?= post_class() ?>"> <h1 class="entry-title">Comments: "<a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>"</h1> <div class="entry-content"> <?php wp_list_comments( 'reverse_top_level=false', get_comments([ 'post_id'=> $post->ID ]) ); comment_form(); ?> </div> </article> <?php get_footer();
IMPORTANT! Now you need to go to the "Permalinks" admin page. It neede for reset the WP rewrite rules.
That's it! A separate page for post comments is ready. Add /comments/ at the end of the URL of the current post and get to the post comments page.