comments_open

Home » Hooks » comments_open

The WordPress comments_open hook is a filter used to check whether comments are allowed on a particular post or page. It returns a boolean value, which is true if comments are open and false if they are closed.

This hook is useful for developers who want to customize the comment section on their WordPress site. By using this hook, they can modify the behavior of the comment section depending on the post or page.

For example, if a developer wants to disable comments on a particular post, they can use the comments_open hook to return a false value. Here is an example usage code:

function disable_comments_on_post_id_123( $open, $post_id ) {
    if ( $post_id == 123 ) {
        return false;
    }
    return $open;
}
add_filter( 'comments_open', 'disable_comments_on_post_id_123', 10, 2 );

In this example, the disable_comments_on_post_id_123 function checks if the post ID is 123 and returns a false value if it is. The add_filter function is used to hook this function to the comments_open filter.

This means that when the comments_open function is called, it will check if the post ID is 123 and return a false value if it is, effectively disabling comments on that post.

Learn More on WordPress.org

Register an account to save your snippets or go Pro to get more features.