Wordpress: Kommentar-Funktion deaktivieren/entfernen

Mit diesem Code Beispiel kann man über PHP im Theme oder Plugin alle Funktionen run um die Wordpress Comments entfernen oder deaktivieren. Der Code sollte in einer eignen Datei liegen und in der functions.php dann required werden.

Diese PHP Klasse schaltet Wordpress Kommentare überall ab:

  1. Es entfernt Wordpress Kommentare aus dem WP-Admin Menu im Backend
  2. Gibt nur leere Kommentarlisten zurück, wenn diese angefragt werden
  3. Leitet den Benutzer im WP-Admin Backend um, wenn dieser probiert direkt aug die Comments-Page zu gehen
  4. Entfernt die den Theme Support für Comments in allen Post Types, auch Custom Post Types

Wie gesagt: dieser Code sollte in eine eigene Datei geschrieben werden und dann mit require_once eingebunden werden.

<?php 
namespace theme\functions\theme_setup;
 
 
DisableComments::do();
 
/**
 * CommentsDisable
 * 
 * Removes all Support for Comments on all places
 * 
 * Usage:
 *   CommentsDisable::do() - Removes Support on all Post types
 
 * Based on SourceCode:
 * @link https://keithgreer.dev/wordpress-code-completely-disable-comments-using-functions-php/
 */
final class DisableComments
{
	private static $class_name = 'theme\functions\theme_setup\DisableComments';
 
	public static function do()
	{
		add_action('admin_init',     [self::$class_name, 'remove_all_post_types_support'] );
		add_filter('comments_open',  [self::$class_name, 'set_comments_status_false'], 20, 2);
		add_filter('pings_open',     [self::$class_name, 'set_comments_status_false'], 20, 2);
		add_filter('comments_array', [self::$class_name, 'hide_existing_comments'], 10, 2);
		add_action('admin_menu',     [self::$class_name, 'disable_comments_admin_menu'] );
		add_action('admin_init',     [self::$class_name, 'redirect_away_from_comments_admin_menu'] );
		add_action('admin_init',     [self::$class_name, 'disable_comments_in_dashboard'] );
	}
 
 
	/**
	 * Removes Support for Comments on all Post Types
	 *
	 * @return void
	 */
	public static function remove_all_post_types_support():void
	{
		$all_post_types = get_post_types();
 
		foreach( $all_post_types as $post_type ) {
			self::remove_post_type_support( $post_type );
		}
	}
 
	/**
	 * Removers Support for Comments on a single Post Type
	 *
	 * @param string $post_type   Default: 'post'
	 * 
	 * @return void
	 */
	private static function remove_post_type_support( string $post_type = 'post' ):void
	{
		$has_comment_support = ( post_type_supports( $post_type, 'comments' ) ) ? true : false;
		if( !$has_comment_support  ) return;
		remove_post_type_support( $post_type, 'comments'   );
		remove_post_type_support( $post_type, 'trackbacks' );
	}
 
 
	/**
	 * Disable any Comments Status Return
	 *
	 * @return boolean
	 */
	public static function set_comments_status_false():bool
	{
		return false;
	}
 
 
	/**
	 * Alwayse returns an empty comment list
	 *
	 * @param array $comments
	 * @return array
	 */
	public static function hide_existing_comments( array $comments ):array
	{
		return [];
	}
 
 
	/**
	 * Removes the Comments Page Menu Item from the WP-Admin Menu
	 *
	 * @return void
	 */
	public static function disable_comments_admin_menu()
	{
		remove_menu_page( 'edit-comments.php' );
	}
 
 
	/**
	 * Redirects the User if he visits the admin comments page directly
	 *
	 * @return void
	 */
	public static function redirect_away_from_comments_admin_menu():void
	{
		global $pagenow;
		$is_current_page_comments = ( $pagenow === 'edit-comments.php' ) ? true : false;
		if( !$is_current_page_comments ) return;
 
		$redirect_target = admin_url();
		wp_redirect( $redirect_target );
		exit;
	}
 
 
	/**
	 * Removes Metabox in the WP-Admin Dashboard
	 *
	 * @return void
	 */
	public static function disable_comments_in_dashboard()
	{
		remove_meta_box( 'dashboard_recent_comments', 'dashboard', 'normal' );
	}
}