Xóa hoàn toàn bình luận khỏi WordPress

Mới đây trong phiên bản WordPress 6.1.1 mình sử dụng dù bạn tắt tính năng bình luận trong WordPress và xóa rỗng code trong file comments.php và không để form comment ở cuối mỗi bài viết thì comment từ đâu vẫn lọt được vào phần quản trị bình luận.

Mình quyết định code 1 số đoạn để xóa toàn bộ tính năng bình luận (comment) khỏi WordPress và dưới đây là đoạn code mà bạn có thể sử dụng để xóa hoàn toàn chức năng bình luận khỏi website.

Bạn chỉ cần copy đoạn code và dán vào file functions.php lưu lại.

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" link from admin bar
function remove_comments_admin_bar() {
    global $wp_admin_bar;
    $wp_admin_bar->remove_menu('comments');
}
add_action('wp_before_admin_bar_render', 'remove_comments_admin_bar');

Ngoài ra như mình nói ở đầu bài, bài có thể mở file comments.php và xóa toàn bộ code trong đó đi để bỏ hoàn toàn chức năng bình luận trong website WordPress, tuy nhiên cách này không khuyến khích mà thường cấu trúc WordPress sẽ được thêm code ở file single.php

if ( comments_open() || get_comments_number() ) :
	comments_template();
endif;

Bạn xóa đoạn code này đi là được.

Nếu bạn không muốn xóa hoàn toàn phần comment mà chỉ muốn xóa phần url ở comment form để giảm spam thì có thể sử dụng đoạn code sau:

add_filter('comment_form_default_fields', 'vts_website_remove');
function vts_website_remove($fields){
if(isset($fields['url']))
unset($fields['url']);
return $fields;
}

1 mẹo để giảm spam nữa là chặn url tự động tạo đường dẫn,

Thêm 1 điều nữa, nếu bạn vẫn muốn giữa lại phần bình luận để hỗ trợ SEO hoặc tăng tương tác thì nên cài thêm plugin Antispam Bee hoặc WP Armour – Honeypot Anti Spam để chặn spam hiệu quả.

Update mới: Có 1 số người hỏi không muốn gỡ hoàn toàn comment mà muốn gỡ mỗi cái Comments topbar thì làm thế nào thì bạn có thể sử dụng đoạn code sau:

function vutruso_remove_admin_bar_links() {
    global $wp_admin_bar;
    $wp_admin_bar->remove_menu('comments');
}
add_action('wp_before_admin_bar_render', 'vutruso_remove_admin_bar_links');

Hy vọng bài viết sẽ giúp ích được cho nhiều người.

Xin cảm ơn.

Nếu bạn thấy bài viết có ích hãy sao chép link và chia sẻ bài viết
user

Yêu thích Võ thuật và Công nghệ thông tin, thích viết và chia sẽ về 2 lĩnh vực này thế thôi :D

Bài viết liên quan