0
X
Add Snippet To Project
New Project
Add To Existing Project
<?php
/**
* Disable comments on the site.
*/
function atar4u_disable_comments() {
// Disable support for comments on all post types
$post_types = get_post_types();
foreach ( $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 all existing posts
$wpdb = $GLOBALS['wpdb'];
$wpdb->query( "UPDATE $wpdb->posts SET comment_status = 'closed'" );
$wpdb->query( "UPDATE $wpdb->posts SET ping_status = 'closed'" );
// Remove comment links from the admin menu
remove_menu_page( 'edit-comments.php' );
remove_submenu_page( 'options-general.php', 'options-discussion.php' );
// Redirect any comment-related URLs to the homepage
if ( is_admin() && isset( $_GET['action'] ) && $_GET['action'] === 'editcomment' ) {
wp_redirect( home_url() );
exit;
}
}
add_action( 'init', 'atar4u_disable_comments' );
Explanation:
This code snippet disables comments on the site by performing the following steps:
The code uses the init action hook to execute the wpturbo_disable_comments function, which performs the necessary actions to disable comments.
Please note that this code should be added to your theme's functions.php file or a custom plugin file.