Skip to content

Instantly share code, notes, and snippets.

@mishterk
Last active July 1, 2020 11:23
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mishterk/ca568c008fe4e5ac16fd1ca3f9eb3f34 to your computer and use it in GitHub Desktop.
Save mishterk/ca568c008fe4e5ac16fd1ca3f9eb3f34 to your computer and use it in GitHub Desktop.
A static class that basically removes all (I think) comment related functionality in WordPress
<?php
namespace PhilKurth\WP\Tools;
/**
* Class DisableComments
*
* Disables the WordPress comment system.
*/
class DisableComments {
static function init()
{
add_action( 'admin_init', [ __CLASS__, 'remove_comments_post_type_supports' ] );
add_filter( 'comments_open', [ __CLASS__, 'close_comments' ], 20, 2 );
add_filter( 'pings_open', [ __CLASS__, 'close_comments' ], 20, 2 );
add_filter( 'comments_array', [ __CLASS__, 'hide_existing_comments' ], 10, 2 );
add_action( 'admin_menu', [ __CLASS__, 'remove_menu_page' ] );
add_action( 'admin_init', [ __CLASS__, 'admin_menu_redirect' ] );
add_action( 'admin_init', [ __CLASS__, 'remove_dashboard_meta_box' ] );
add_action( 'init', [ __CLASS__, 'remove_from_admin_bar' ] );
}
/**
* Disable support for comments and trackbacks in post types
*/
static function remove_comments_post_type_supports()
{
foreach ( get_post_types_by_support( 'comments' ) as $postType ) {
remove_post_type_support( $postType, 'comments' );
}
}
/**
* Close comments on the front-end
* @return bool
*/
static function close_comments()
{
return false;
}
/**
* Hide existing comments
* @param $comments
* @return array
*/
static function hide_existing_comments( $comments )
{
$comments = array();
return $comments;
}
/**
* Remove comments page in menu
*/
static function remove_menu_page()
{
remove_menu_page( 'edit-comments.php' );
}
/**
* Redirect any user trying to access comments page
*/
static function admin_menu_redirect()
{
global $pagenow;
if ( $pagenow === 'edit-comments.php' ) {
wp_redirect( admin_url() );
exit;
}
}
/**
* Remove comments metabox from dashboard
*/
static function remove_dashboard_meta_box()
{
remove_meta_box( 'dashboard_recent_comments', 'dashboard', 'normal' );
}
/**
* Remove comments links from admin bar
*/
static function remove_from_admin_bar()
{
if ( is_admin_bar_showing() ) {
remove_action( 'admin_bar_menu', 'wp_admin_bar_comments_menu', 60 );
}
}
}
<?php
include 'class-disable-comments.php';
\PhilKurth\WP\Tools\DisableComments::init();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment