This repository has been archived by the owner on Oct 1, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathstorm-comments-page.php
68 lines (57 loc) · 1.9 KB
/
storm-comments-page.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
<?php
/*
Plugin Name: Storm Separate Comments Page
Description: Generate a separate page for comments
Version: 1
Author: Storm Consultancy
Author URI: http://www.stormconsultancy.co.uk
*/
// Tell WordPress about our new query variable
function comments_endpoint_query_vars($vars){
$vars[] = "comments";
return $vars;
}
add_filter( 'query_vars', 'comments_endpoint_query_vars');
// Add a /comments/ page to all post permalinks
function comments_endpoint_add_endpoint() {
add_rewrite_endpoint( 'comments', EP_PERMALINK );
}
add_action( 'init', 'comments_endpoint_add_endpoint' );
// Change the template used when the permalink has /comments/
function comments_endpoint_template_redirect($templates = "") {
global $wp_query;
if(!isset( $wp_query->query['comments'] ))
return $templates;
$templates = locate_template( "comments-single.php", false );
if( empty($templates) ) { $templates = dirname(__FILE__).'/comments-single.php'; }
return $templates;
}
add_action( 'single_template', 'comments_endpoint_template_redirect' );
// Update comment permalinks
function comments_endpoint_get_comment_link($url)
{
$urlparts = explode("#", $url);
return $urlparts[0] . 'comments/#' . $urlparts[1];
}
add_filter('get_comment_link', 'comments_endpoint_get_comment_link');
// Update the page title
function comments_endpoint_wp_title( $title ) {
global $wp_query;
if( isset( $wp_query->query['comments'] ) ) {
return "Comments for " . $title;
} else {
return $title;
}
}
add_filter( 'wp_title', 'comments_endpoint_wp_title', 10, 1 );
// Apply and remove the rewrite rules with plugin activation
function comments_endpoint_activate() {
comments_endpoint_add_endpoint();
flush_rewrite_rules();
}
register_activation_hook( __FILE__, 'comments_endpoint_activate' );
function comments_endpoint_deactivate() {
flush_rewrite_rules();
}
register_deactivation_hook( __FILE__, 'comments_endpoint_deactivate' );
?>