Bài viết này thực hiện (hoặc lụm bài về đăng câu like từ các trang khác) bởi Việt Lâm Coder một YOUTUBER có tâm và đẹp trai siêu cấp vô địch zũ trụ.
In this article, we’re going to show you how to disable comments on a WordPress website without the need for a plugin.
The WordPress content management system was initially created for bloggers, and the commenting system was mandatory for that application. However, as it grew, many people began using it for other purposes like corporate websites, which don’t need comments. In fact, if you keep comments enabled, you’re opening yourself up to spam on these type of websites.
Because of that, it’s important to understand how to disable comments on a WordPress website. There are many plugins that help you do just that, but if you are looking for a method that doesn’t use a plugin (adding more bulk to your site) there’s an easy code snippet that you can use. In this article, we’re going to show you that code snippet, and how to implement it into your WordPress website, closing your comments, and eliminating any potential for spam.
Code Snippet To Disable WordPress Comments
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
<?php add_action('admin_init', function () { // Redirect any user trying to access comments page global $pagenow; if ($pagenow === 'edit-comments.php') { wp_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('init', function () { if (is_admin_bar_showing()) { remove_action('admin_bar_menu', 'wp_admin_bar_comments_menu', 60); } }); |
How To Use It
All you need to do is copy and paste the source code directly into a code snippet, or your functions.php file. For script management, we highly recommend the Advanced Scripts tool. Simply copy and paste the code into a new advanced scripts PHP snippet, and hook into the init.
If you use Advanced Scripts 2.0, you can download this file and import the script directly into your website. All you need to do is toggle it on, and you’re good to go. No more comments on your WordPress website.
Bài viết này thực hiện (hoặc lụm bài về đăng câu like từ các trang khác) bởi Việt Lâm Coder một YOUTUBER có tâm và đẹp trai siêu cấp vô địch zũ trụ.