Skip to content

Instantly share code, notes, and snippets.

@EarthlingDavey
Forked from mattclements/function.php
Created January 30, 2020 15:27
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save EarthlingDavey/bc72015b23449a10e4476c2fe6a63fec to your computer and use it in GitHub Desktop.
Save EarthlingDavey/bc72015b23449a10e4476c2fe6a63fec to your computer and use it in GitHub Desktop.
Wordpress Disable Comments (add to function.php)
<?php
/**
* Class Comments.
* Code refactored from:
* https://gist.github.com/mattclements/eab5ef656b2f946c4bfb
*
* @package Foo
*/
namespace Boop;
/**
* Class Comments - actions and filters related to WordPress comments.
*
* @package Foo
*/
class Comments {
/**
* Static function must be called after require within functions.php
* This will setup all action and filter hooks related to comments.
*/
public static function initialise() {
$self = new self();
// Admin actions.
add_action( 'admin_init', array( $self, 'disable_comments_post_types_support' ) );
add_action( 'admin_init', array( $self, 'disable_comments_dashboard' ) );
add_action( 'admin_init', array( $self, 'disable_comments_admin_menu_redirect' ) );
add_action( 'admin_menu', array( $self, 'disable_comments_admin_menu' ) );
add_action( 'wp_before_admin_bar_render', array( $self, 'admin_bar_render' ) );
// Frontend.
add_action( 'init', array( $self, 'disable_comments_admin_bar' ) );
// Close comments on the front-end.
add_filter( 'comments_open', '__return_false', 20, 2 );
add_filter( 'pings_open', '__return_false', 20, 2 );
// Hide existing comments.
add_filter( 'comments_array', '__return_empty_array', 10, 2 );
}
/** Disable support for comments and trackbacks in post types. */
public function disable_comments_post_types_support() {
$post_types = get_post_types();
foreach ( $post_types as $post_type ) {
if ( post_type_supports( $post_type, 'comments' ) ) {
remove_post_type_support( $post_type, 'comments' );
remove_post_type_support( $post_type, 'trackbacks' );
}
}
}
/** Remove comments page in menu. */
public function disable_comments_admin_menu() {
remove_menu_page( 'edit-comments.php' );
}
/** Redirect any user trying to access comments page. */
public function disable_comments_admin_menu_redirect() {
global $pagenow;
if ( 'edit-comments.php' === $pagenow ) {
wp_safe_redirect( admin_url() );
exit;
}
}
/** Remove comments metabox from dashboard. */
public function disable_comments_dashboard() {
remove_meta_box( 'dashboard_recent_comments', 'dashboard', 'normal' );
}
/** Remove comments links from admin bar. */
public function disable_comments_admin_bar() {
if ( is_admin_bar_showing() ) {
remove_action( 'admin_bar_menu', 'wp_admin_bar_comments_menu', 60 );
}
}
/** Remove comments links from admin bar. */
public function admin_bar_render() {
global $wp_admin_bar;
$wp_admin_bar->remove_menu( 'comments' );
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment