screenshot-1

Plugin to Remove Comments Completely from WordPress

The comment feature in WordPress is an essential component of blogs - but not in all cases it's needed, especially if you use it as a traditional CMS.

Sure, you can just leave out the comment form while you creating a theme and disable the comment options in your backend. But this is not the best way for everyone. In such a case, I turn the comments off completely, making sure that the posts can not have that option and also remove the fields for the comments in the backend. That way the user gets exactly what he needs. Meanwhile, I have needed this so often that I created a small Plugin and I'm able to immediately „switch off“ the functions and comment areas. If there are major adaptations, I create them via Plugin or use my existing Plugin Adminimize.

One or the other will have some ideas or know some improvements for the Plugin - go to github and fork it or improve it, I would like to hear some feedback.
In the following screenshot I marked a few areas that are gone now. In the content editor of posts, pages or other post types the Metaboxes for comments, discussion and trackbacks are gone. More screenshots, what else is gone can be seen on my Repository.

On Dashboard without comments

Download link and links to the latest code are on github: Bueltge / Remove-Comments-Absolute. The following code is for a quick reading and represents only the first level. Updates or maintenance will take place on GitHub.

<?php
/**
 * Plugin Name: Remove Comments Absolutely
 * Plugin URI: http://bueltge.de/
 * Text Domain: remove_comments_absolute
 * Domain Path: /languages
 * Description: Deactivate comments functions and remove areas absolutely from the WordPress install
 * Author: Frank Bültge
 * Version: 0.0.2
 * Licence: GPLv2
 * Author URI: http://bueltge.de
 * Upgrade Check: none
 * Last Change: 08.06.2011
 */

if ( ! class_exists( 'Remove_Comments_Absolute' ) ) {
	add_action( 'plugins_loaded', array( 'Remove_Comments_Absolute', 'get_object' ) );
	
	class Remove_Comments_Absolute {
		
		static private $classobj = NULL;
		
		/**
		 * Constructor, init on defined hooks of WP and include second class
		 * 
		 * @access  public
		 * @since   0.0.1
		 * @uses    add_filter, add_action
		 * @return  void
		 */
		public function __construct () {
			
			add_filter( 'the_posts', array( $this, 'set_comment_status' ) );
			
			add_filter( 'comments_open', array( $this, 'close_comments'), 10, 2 );
			add_filter( 'pings_open', array( $this, 'close_comments'), 10, 2 );
			
			add_action( 'admin_init', array( $this, 'remove_comments' ) );
			add_action( 'admin_menu', array( $this, 'remove_menu_items' ) );
			add_filter( 'add_menu_classes', array( $this, 'add_menu_classes' ) );
			
			add_action( 'admin_head', array( $this, 'remove_comments_areas' ) );
			
			add_action( 'wp_before_admin_bar_render', array( $this, 'admin_bar_render' ) );
		}
		
		/**
		 * Handler for the action 'init'. Instantiates this class.
		 * 
		 * @access  public
		 * @since   0.0.1
		 * @return  object $classobj
		 */
		public function get_object () {
			
			if ( NULL === self :: $classobj ) {
				self :: $classobj = new self;
			}
			
			return self :: $classobj;
		}
		
		/**
		 * Set the status on posts and pages - is_singular ()
		 * 
		 * @access  public
		 * @since   0.0.1
		 * @uses    is_singular
		 * @param   string $posts
		 * @return  string $posts
		 */
		public function set_comment_status ( $posts ) {
			
			if ( ! empty( $posts ) && is_singular() ) {
				$posts[0]->comment_status = 'closed';
				$posts[0]->post_status = 'closed';
			}
			
			return $posts;
		}
		
		/**
		 * Close comments, if open
		 * 
		 * @access  public
		 * @since   0.0.1
		 * @param   string | boolean $open
		 * @param   string | integer $post_id
		 * @eturn  string $posts
		 */
		public function close_comments ( $open, $post_id ) {
			// if not open, than back
			if ( ! $open )
				return $open;
			
			$post = get_post( $post_id );
			if ( $post -> post_type ) // all post types
				return FALSE;
			
			return $open;
		}
		
		/**
		 * Change options for dont use comments
		 * Remove meta boxes on edit pages
		 * Remove support on all post types for comments
		 * 
		 * @access  public
		 * @since   0.0.1
		 * @uses    update_option, get_post_types, remove_meta_box, remove_post_type_support
		 * @return  void
		 */
		public function remove_comments () {
			// int values
			foreach ( array( 'comments_notify', 'default_pingback_flag' ) as $option )
				update_option( $option, 0 );
			// string false
			foreach ( array( 'default_comment_status', 'default_ping_status' ) as $option )
				update_option( $option, 'false' );
			
			// all post types
			// alternative define an array( 'post', 'page' )
			foreach ( get_post_types() as $post_type ) {
				// comment status
				remove_meta_box( 'commentstatusdiv', $post_type, 'normal' );
				// remove trackbacks
				remove_meta_box( 'trackbacksdiv', $post_type, 'normal' );
				// remove all comments/trackbacks from tabels
				remove_post_type_support( $post_type, 'comments' );
				remove_post_type_support( $post_type, 'trackbacks' );
			}
			// remove dashboard meta box for recents comments
			remove_meta_box( 'dashboard_recent_comments', 'dashboard', 'normal' );
		}
		
		/**
		 * Remove menu-entries
		 * 
		 * @access  public
		 * @since   0.0.2
		 * @uses    remove_meta_box, remove_post_type_support
		 * @return  void
		 */
		public function remove_menu_items () {
			// Remove menu entries with WP 3.1 and higher
			if ( function_exists( 'remove_menu_page' ) ) {
				remove_menu_page( 'edit-comments.php' );
				remove_submenu_page( 'options-general.php', 'options-discussion.php' );
			} else {
				// unset comments
				unset( $GLOBALS['menu'][25] );
				// unset menuentry Discussion
				unset( $GLOBALS['submenu']['options-general.php'][25] );
			}
		}
		
		/**
		 * Add class for last menu entry with no 20
		 * 
		 * @access  public
		 * @since   0.0.1
		 * @param   array string $menu
		 * @return  array string $menu
		 */
		function add_menu_classes ( $menu ) {
			
			$menu[20][4] .= ' menu-top-last';
			
			return $menu;
		}
		
		/**
		 * Remove areas for comments in backend via JS
		 * 
		 * @access  public
		 * @since   0.0.1
		 * $return  string with js
		 */
		public function remove_comments_areas () {
			?>
			<script type="text/javascript">
			//<![CDATA[
			jQuery(document).ready( function($) {
				$( '.table_discussion' ).remove();
			});
			//]]>
			</script>
			<?php
		}
		
		/**
		 * Remove comment entry in Admin Bar
		 * 
		 * @access  public
		 * @since   0.0.1
		 * @uses    remove_menu
		 * $return  void
		 */
		public function admin_bar_render () {
			// remove entry in admin bar
			$GLOBALS['wp_admin_bar'] -> remove_menu( 'comments' );
		}
		
	
	} // end class

} // end if class exists
?>

on write post

Comments are closed.

16 comments

  1. Joss Crowcroft

    This is great - well done!

  2. Paul

    thanks, I was actually wishing for this type of plugin which makes a lot of sense to use on a site that doesn't have a blog!

  3. Paul

    hey, I'm seeing this error on the frontend, on a custom single custom post type template :
    Warning: call_user_func_array() [function.call-user-func-array]: First argument is expected to be a valid callback, 'Array' was given in /home/..../public_html/deepfry/wp-includes/plugin.php on line 166

  4. noCopyPaste

    hey dude, steal your text, and do not mention

    http://wpengineer.com/2230/removing-comments-absolutely-wordpress/
    in spanish
    http://translate.google.com.mx/translate?js=n&prev=_t&hl=es&ie=UTF-8&layout=2&eotf=1&sl=es&tl=en&u=http%3A%2F%2Fayudawordpress.com%2Feliminar-completamente-los-comentarios%2F

    the site is:
    http://ayudawordpress.com/eliminar-completamente-los-comentarios/

  5. Alex

    Thanks dude, I did leave a comment that they should leave a source, where the article is coming from.

    Appreciate telling us!

  6. Frank

    @Paul: i have fixed this, was an cracy error of my mind :( But please check always the status on github for updates and also add issues on github, its easy to track for all and for me. Thanks

  7. Paul

    thanks Frank, why don't you host it on the WordPress repository?

  8. Frank

    @Paul: ist harder to publish on svn, git is realy better - but i have so much plugins on wp.org and its to much support for me; i have fear for to much plugins and support. Maybe i will do this and host on wp.org

  9. george

    You say it removes "completely", but what about the comments feed associated with each post ?

    <link rel="alternate" type="application/rss+xml"...

    And what about the blog feed, which contains many tags ?

  10. Frank

    @george: Many Thanks - you have right; but now i have add a function to do this and also i open a ticket on WP Trac for change core; the plugin here add only all default feeds, without comments; but this also an small question in the core of WP, if the comments is inactive, than it is important to add not a comment feed.
    Current use the last version on github and the feed is not include of head.

  11. dan

    I installed the plugin. it seems to have removed some features, but all post have "closed for comments" And front page has # comments feature still showing- which is what I am trying to get rid of. Any advice?

  12. george

    @dan: These messages are controlled by your theme. The easiest way is to edit .CSS (style sheet) file of your theme, and to hide them:

    #comments {display:none;}
    .page .comments-closed {display:none;}

    The names "#comments" and ".page .comments-closed" are just an example. In your web browser, look at the source code of your front page and post page, to determine the exact names to hide.

  13. Kate Brisbane

    It's a tricky one when you want certain comments to show and not others.. Good of you to grow WordPress -it keeps getting better as we specify the needs.

  14. Alejandro - España

    I will try this procedure on my blog. Thank you.

2 pingbacks

  1. WordPress Code Snippets for Twitter and Comments - WordPress Consulting by Christopher Ross
  2. WordPress: Kommentare nachträglich deaktivieren | WordPress-Buch