Disable comments

Closes comments and pingbacks everywhere, hides existing ones and removes Comments from wp-admin. WooCommerce product reviews stay by default.

Hook
comments_open
Updated
2026-09-27
License
GPLv2
add_action( 'init', function() {
    $keep = array_filter( array_map( 'trim', explode( ',', 'product' ) ) );
    $off  = fn( $post_id ) => ! in_array( get_post_type( $post_id ), $keep, true );

    foreach ( get_post_types() as $type ) {
        if ( ! in_array( $type, $keep, true ) ) {
            remove_post_type_support( $type, 'comments' );
            remove_post_type_support( $type, 'trackbacks' );
        }
    }
    add_filter( 'comments_open', fn( $open, $id ) => $off( $id ) ? false : $open, 20, 2 );
    add_filter( 'pings_open', fn( $open, $id ) => $off( $id ) ? false : $open, 20, 2 );
    add_filter( 'comments_array', fn( $comments, $id ) => $off( $id ) ? array() : $comments, 20, 2 );
    add_filter( 'feed_links_show_comments_feed', '__return_false' );

    // Product reviews have their own screen under Products, so the Comments screen can go.
    if ( array_diff( $keep, array( 'product' ) ) ) {
        return;
    }
    add_action( 'admin_menu', fn() => remove_menu_page( 'edit-comments.php' ) );
    add_action( 'admin_bar_menu', fn( $bar ) => $bar->remove_node( 'comments' ), 60 );
    add_action( 'wp_dashboard_setup', fn() => remove_meta_box( 'dashboard_recent_comments', 'dashboard', 'normal' ) );
    add_action( 'admin_init', function() {
        global $pagenow;
        if ( 'edit-comments.php' === $pagenow ) {
            wp_safe_redirect( admin_url() );
            exit;
        }
    } );
}, 100 );

These are the default values. Change them in the builder before you copy. Licensed under GPL-2.0-or-later, provided as is. Review and test it before using it on a live site.

How it works

The snippet removes comment and trackback support from every post type, then closes comments and pings with the comments_open and pings_open filters, including on posts that were open before. Existing comments are hidden with comments_array, and the comments feed link leaves the page head. When no post type keeps its comments, the Comments menu, its admin bar icon and the dashboard widget go too, and edit-comments.php redirects to the dashboard.

Before you use it

  • In WooCommerce, product reviews are comments. The default keeps them on product, which has its own Reviews screen under Products.
  • To keep comments somewhere else, add its post type to the list, separated by commas: product, post. Leave it empty to turn comments off everywhere.
  • Nothing is deleted. Old comments stay in the database, just hidden.

How to check it works

Open a blog post in a private window. The comment form and the old comments should be gone, and Comments should be missing from the admin menu.

How to undo it

Remove the code. Comments, and the settings each post had, come back as they were.

How to add this snippet

  1. Copy the code above.
  2. Paste it at the end of your child theme’s functions.php, in Appearance → Theme File Editor or over FTP.
  3. Save, then open the site and check that everything works. If something breaks, remove the code.

Need more than one? Open this snippet in the builder, turn on the others you want and copy them as one functions.php, or download them as a small plugin.

More in Admin