Skip to content

Instantly share code, notes, and snippets.

@glueckpress
Last active April 2, 2021 20:35
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 glueckpress/ec846d50ba9825a24fc3 to your computer and use it in GitHub Desktop.
Save glueckpress/ec846d50ba9825a24fc3 to your computer and use it in GitHub Desktop.
[WordPress] This plugin started as a Gist, but can now be found at https://github.com/glueckpress/no-comment.
<?php
/**
* Plugin Name: No Comment
* Description: A plugin to close, disable and remove comments from the WordPress admin UI.
* Version: 0.1
* Author: Caspar Hübinger
* Author URI: https://profiles.wordpress.org/glueckpress/
* License: GNU General Public License v2 or later
* License URI: http://www.gnu.org/licenses/gpl-2.0.html
*
* Requirements: PHP 5.3+, WordPress 3.8+
*/
if( ! defined( 'ABSPATH' ) )
exit;
/**
* Disable Comments Everywhere.
*
* @return void
*/
function no_comment() {
/**
* Set existing comments to unapproved.
*
* @return void
*/
add_action( 'after_setup_theme', function () {
$comments = get_comments();
foreach ( $comments as $comment ) {
$commentarr = array(
'comment_ID' => $comment->comment_ID,
'comment_approved' => 0
);
wp_update_comment( $commentarr );
}
}, 1000 );
/**
* Close comments on all published posts of all post types.
*
* @return void
*/
add_action( 'after_setup_theme', function () {
$posts = get_posts( array( 'post_type' => 'any', 'posts_per_page' => -1 ) );
foreach ( $posts as $post ) {
if ( $post->comment_status !== 'closed' ) {
$postarr = array(
'ID' => $post->ID,
'comment_status' => 'closed'
);
wp_update_post( $postarr );
}
}
}, 0 );
/**
* Set default comment status to closed.
*
* @return void
*/
add_action( 'after_setup_theme', function () {
if( 'closed' !== get_option( 'default_comment_status' ) ) {
update_option( 'default_comment_status', 'closed' );
}
} );
/**
* Remove comments and discussion menu pages from admin menu.
*
* @return void
*/
add_action( 'admin_menu', function () {
remove_menu_page( 'edit-comments.php' );
remove_submenu_page( 'options-general.php', 'options-discussion.php' );
} );
/**
* Remove comment menu item from admin bar.
*
* @return void
*/
add_action( 'wp_before_admin_bar_render', function () {
$GLOBALS[ 'wp_admin_bar' ]->remove_menu( 'comments' );
} );
/**
* Remove comments post type support from all registered post types.
*
* @return void
*/
add_action( 'init', function () {
$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' );
}
}, 0 );
/**
* Remove default comments widget.
*
* @return void
*/
add_action( 'widgets_init', function() {
unregister_widget( 'WP_Widget_Recent_Comments' );
}, 0 );
/**
* Remove comments from the Dashboard.
*
* @return void
*/
add_action( 'wp_dashboard_setup', function () {
remove_meta_box( 'dashboard_recent_comments', 'dashboard', 'normal' );
remove_meta_box( 'dashboard_activity', 'dashboard', 'normal');
wp_add_dashboard_widget(
'no_comment__dashboard_activity',
__( 'Activity' ),
function () {
echo '<div id="activity-widget">';
$future_posts = wp_dashboard_recent_posts( array(
'max' => 5,
'status' => 'future',
'order' => 'ASC',
'title' => __( 'Publishing Soon' ),
'id' => 'future-posts',
) );
$recent_posts = wp_dashboard_recent_posts( array(
'max' => 5,
'status' => 'publish',
'order' => 'DESC',
'title' => __( 'Recently Published' ),
'id' => 'published-posts',
) );
if ( ! $future_posts && ! $recent_posts ) {
echo '<div class="no-activity">';
echo '<p class="smiley"></p>';
echo '<p>' . __( 'No activity yet!' ) . '</p>';
echo '</div>';
}
echo '</div>';
}
);
} );
}
add_action( 'plugins_loaded', 'no_comment' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment