-
-
Save EarthlingDavey/bc72015b23449a10e4476c2fe6a63fec to your computer and use it in GitHub Desktop.
Wordpress Disable Comments (add to function.php)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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