Skip to content

Instantly share code, notes, and snippets.

@brichards
Created August 7, 2019 20:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save brichards/1468a7defa129fdcf1e76e8aefb3c0ca to your computer and use it in GitHub Desktop.
Save brichards/1468a7defa129fdcf1e76e8aefb3c0ca to your computer and use it in GitHub Desktop.
Plugin to make WordPress comments available at <post_url>/comments/.
<?php
/**
* Plugin Name: Comments Page
* Plugin URI: https://WPSessions.com
* Description: Makes comments for a post accessible at <post_url>/comments/.
* Author: Brian Richards
* Author URI: https://rzen.net
* Text Domain: comments-page
* Domain Path: /languages
* Version: 0.1.0
*/
namespace CommentsPage;
// Original props to https://github.com/stormuk/wp-comments-page for the source material.
/**
* Register endpoint and flush rewrite during activation.
*/
function activation_hook() {
comments_endpoint_add_endpoint();
flush_rewrite_rules();
}
register_activation_hook( __FILE__, __NAMESPACE__ . '\activation_hook' );
/**
* Flush rewrite rules during deactivation to remove custom endpoint.
*/
function deactivation_hook() {
flush_rewrite_rules();
}
register_deactivation_hook( __FILE__, __NAMESPACE__ . '\deactivation_hook' );
/**
* Register the /comments/ endpoint for all post permalinks.
*/
function add_comments_endpoint() {
add_rewrite_endpoint( 'comments', EP_PERMALINK );
}
add_action( 'init', __NAMESPACE__ . '\add_comments_endpoint' );
/**
* Make 'comments' an accessible query_var.
*
* @param array $vars Registered query_vars.
* @return array Updated query_vars.
*/
function add_comments_query_var( $vars ) {
$vars[] = 'comments';
return $vars;
}
add_filter( 'query_vars', __NAMESPACE__ . '\add_comments_query_var' );
/**
* Change the template used when the permalink includes /comments/.
*
* @param string $templates
* @return string
*/
function register_comments_template( $templates = '' ) {
global $wp_query;
if ( ! isset( $wp_query->query['comments'] ) ) {
return $templates;
}
$templates = locate_template( 'single-comments.php', false );
if ( empty( $templates ) ) {
$templates = dirname( __FILE__ ) . '/single-comments.php';
}
return $templates;
}
add_action( 'single_template', __NAMESPACE__ . '\register_comments_template' );
/**
* Update individual comment permalink to include endpoint.
*
* @param string $link The comment permalink with '#comment-$id' appended.
* @param WP_Comment $comment The current comment object.
* @param array $args An array of arguments to override the defaults.
* @param int $cpage The calculated comment page value.
*/
function get_comment_link( $link, $comment, $args, $cpage ) {
return get_comments_link( $link, $comment->comment_post_ID ) . '#comment-' . $comment->comment_ID;
}
add_filter( 'get_comment_link', __NAMESPACE__ . '\get_comment_link', 10, 4 );
/**
* Update comments link to point to comments permalink page.
*
* @param string $link The current comments link.
* @param int $post_id The current post ID.
* @return string Updated comments permalink.
*/
function get_comments_link( $link, $post_id ) {
return untrailingslashit( get_permalink( $post_id ) ) . '/comments/';
}
add_filter( 'respond_link', __NAMESPACE__ . '\get_comments_link', 10, 2 );
add_filter( 'get_comments_link', __NAMESPACE__ . '\get_comments_link', 10, 2 );
/**
* Update the page title when viewing a comment permalink.
*
* @param string $title
* @return string
*/
function update_page_title( $title ) {
global $wp_query;
if ( isset( $wp_query->query['comments'] ) ) {
$title = sprintf( __( 'Comments for %s' ), $title );
}
return $title;
}
add_filter( 'wp_title', __NAMESPACE__ . '\update_page_title' );
<?php get_header(); ?>
<div id="primary" class="content-area">
<main id="main" class="site-main" role="main">
<h1><?php printf( __( 'Comments for <a href="%1$s">%2$s</a>', 'comments-page' ), get_permalink(), get_the_title() ); ?></h1>
<?php
if ( comments_open() || get_comments_number() ) :
comments_template();
endif;
?>
</main><!-- #main -->
</div><!-- #primary -->
<?php get_sidebar(); ?>
<?php get_footer(); ?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment