How to disable comments in WordPress using a snippet

Last updated December 6th, 2023 00:01

Comments are an integral part of the WordPress blogging platform. They allow users on your website to interact with the content you present. In the case of a blog, such comments are desirable as they help build a community and provide users with the opportunity to ask questions or express their opinions about your website and its content. However, there are situations where comments are not desired. These could be business presentations or pages unrelated to the blog where comments are not suitable. Fortunately, comments can be easily disabled. Here I will show you how to disable comments in WordPress using a snippet.

How to Disable Comments in WordPress Using a Snippet

To be able to add short PHP codes, also known as snippets, to your website, you will need a plugin. There are several available in the official repository. Personally, I recommend using the WPCode plugin. It is also available at this address: https://wordpress.org/plugins/insert-headers-and-footers/

Thanks to this plugin, you can easily add code to the system that will run and perform actions and functions defined by you. For detailed instructions, if you have never worked with such a plugin before, you can find them here: How to Insert Custom Code in WordPress

If you have read the article, you probably already have an idea of how to add such a snippet to your website. The code to completely disable comments looks like this:

				
					add_action('admin_init', function () {
    // Redirect any user trying to access comments page
    global $pagenow;
    
    if ($pagenow === 'edit-comments.php') {
        wp_safe_redirect(admin_url());
        exit;
    }

    // Remove comments metabox from dashboard
    remove_meta_box('dashboard_recent_comments', 'dashboard', 'normal');

    // Disable support for comments and trackbacks in post types
    foreach (get_post_types() as $post_type) {
        if (post_type_supports($post_type, 'comments')) {
            remove_post_type_support($post_type, 'comments');
            remove_post_type_support($post_type, 'trackbacks');
        }
    }
});

// Close comments on the front-end
add_filter('comments_open', '__return_false', 20, 2);
add_filter('pings_open', '__return_false', 20, 2);

// Hide existing comments
add_filter('comments_array', '__return_empty_array', 10, 2);

// Remove comments page in menu
add_action('admin_menu', function () {
    remove_menu_page('edit-comments.php');
});

// Remove comments links from admin bar
add_action('admin_bar_menu', function () {
    remove_action('admin_bar_menu', 'wp_admin_bar_comments_menu', 60);
}, 0);
				
			

Conclusion

Once you add this code to your website and activate the snippet using the WPCode plugin, comments will be disabled across the entire website and its structure. If comments are undesirable for you, this provides a convenient and straightforward solution.

More content about WordPress

Whole category is here

The website is created with care for the included information. I strive to provide high-quality and useful content that helps or inspires others. If you are satisfied with my work and would like to support me, you can do so through simple options.

Byl pro Vás tento článek užitečný?

Klikni na počet hvězd pro hlasování.

Průměrné hodnocení. 0 / 5. Počet hlasování: 0

Zatím nehodnoceno! Buďte první

Jak užitečný vidíte tento článek.

Sledujte mě na sociálních médiích.

Je mi líto, že pro Vás nebyl článek užitečný.

Jak mohu vylepšit článek?

Řekněte mi, jak jej mohu zlepšit.

newsletter

Subscribe to the Newsletter

Stay informed! Join our newsletter subscription and be the first to receive the latest information directly to your email inbox. Follow updates, exclusive events, and inspiring content, all delivered straight to your email.

Odebírat
Upozornit na
guest
0 Komentáře/ů
Vložené zpětné vazby.
Zobrazit všechny komentáře.

Pokud mi chcete napsat rychlou zprávu, využije, prosím, níže uvedený
kontaktní formulář. Děkuji.

Další Kontaktní údaje