Home » Functions » comments_open

The comments_open() function in WordPress is a conditional tag that checks whether comments are open for a particular post or page. This function returns a Boolean value of true if comments are open or false if they are not.

WordPress provides an easy way to enable or disable comments on individual posts or pages. By default, comments are enabled on new posts and pages, but you can turn them off by unchecking the Allow Comments box in the Discussion section of the post editor.

The comments_open() function comes in handy when you want to display or hide certain content based on whether comments are open or closed. For example, you may want to display a message encouraging users to leave a comment if comments are open, or display a message letting users know that comments are closed.

Here is an example of how you can use the comments_open() function in your WordPress theme:

<?php if (comments_open()) : ?>
    <p>Leave a comment:</p>
    <?php comment_form(); ?>
<?php else : ?>
    <p>Comments are closed.</p>
<?php endif; ?>

In this example, the code checks if comments are open for the current post using the comments_open() function. If comments are open, it displays a simple message and the comment form using the comment_form() function. If comments are closed, it displays a message letting users know that comments are closed.

Overall, the comments_open() function is a useful tool for WordPress developers who want to customize the display of comments on their website.

Learn More on

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