To disable the comment section under posts in WordPress using the functions.php file, you can use the comments_open filter. This filter allows you to control whether comments are open or closed for individual posts. To disable comments site-wide for all posts, you can set the comments_open value to false for every post. Here's how you can do it:
-
Access your WordPress theme's functions.php file. You can find this file in the theme's folder.
-
Add the following code at the end of the functions.php file:
function disable_comments_on_posts( $open, $post_id ) {
// Check if it's a post - www.websitefunctions.com
if ( get_post_type( $post_id ) === 'post' ) {
return false; // Disable comments for posts
}
return $open;
}
add_filter( 'comments_open', 'disable_comments_on_posts', 10, 2 );
- Save the changes to the functions.php file.
Now, the comment section should be disabled for all posts on your WordPress website.
Please note that this code will disable comments for all posts, including existing ones and any new posts you create in the future. If you only want to disable comments on new posts but keep existing ones intact, you can manually disable comments in the WordPress admin panel for each post.
Keep in mind that modifying the functions.php file can impact your theme's functionality, so it's always a good practice to create a backup of the file before making any changes. If you are not comfortable with editing the functions.php file directly, consider using a child theme to make the modifications. This way, your changes won't be lost when the theme is updated.