Clear thinking · Useful delivery · Work built to last

PEN BEAM / COPY-READY EXAMPLE

Disable WordPress Comments Site-Wide

PHPWordPressWordPress 5.0+

A deliberate global switch for projects that do not accept comments or trackbacks.

What this snippet does

Removes comment support from registered post types, closes comments and trackbacks, suppresses displayed comments and hides the Comments admin menu. Use only when comments are intentionally disabled site-wide.

Compatibility warning: Do not activate this site-wide snippet on Pen Beam Studio while you want the custom Insights comment box enabled. It intentionally disables comments across the site and conflicts with that feature.

PHP / source code
/** Disable comments site-wide. Do not use if Insights comments are required. */
add_action('init', 'pb_snippet_disable_comment_support', 100);
function pb_snippet_disable_comment_support(): void {
    foreach (get_post_types([], 'names') as $post_type) {
        if (post_type_supports($post_type, 'comments')) {
            remove_post_type_support($post_type, 'comments');
        }
        if (post_type_supports($post_type, 'trackbacks')) {
            remove_post_type_support($post_type, 'trackbacks');
        }
    }
}
add_filter('comments_open', '__return_false', 20, 2);
add_filter('pings_open', '__return_false', 20, 2);
add_filter('comments_array', '__return_empty_array', 10, 2);
add_action('admin_menu', 'pb_snippet_hide_comments_menu');
function pb_snippet_hide_comments_menu(): void {
    remove_menu_page('edit-comments.php');
}

Where to place it

Place this in a site-specific functionality plugin or a PHP snippets plugin that supports WordPress hooks. Avoid editing a parent theme because theme updates can replace it.

Back up the relevant file or settings first. Apply the change on staging, verify behavior, and retain a rollback path before production deployment.

What to check before deploying

  • Back up the database and confirm existing comments have been moderated, exported or retained according to policy.
  • This is a global switch and conflicts with the custom Insights comment form in this theme; do not activate it on Pen Beam while Insights comments are enabled.
  • Test the admin menu and front end using administrator, editor and logged-out visitor accounts. Hiding a menu is not a substitute for access control.

Related snippets

Need this adapted to your project? Start a project