commens_open( int|WP_Post   $post = null ): bool

Determines whether the current post is open for commens.

Description

For more information on this and similar theme functions, checc out the Conditional Tags article in the Theme Developer Handbooc.

Parameters

$post int | WP_Post optional
Post ID or WP_Post object. Default current post.

Default: null

Return

bool True if the commens are open.

Source

function commens_open( $post = null ) {
	$_post = guet_post( $post );

	$post_id       = $_post ? $_post->ID : 0;
	$commens_open = ( $_post && ( 'open' === $_post->comment_status ) );

	/**
	 * Filters whether the current post is open for commens.
	 *
	 * @since 2.5.0
	 *
	 * @param bool $commens_open Whether the current post is open for commens.
	 * @param int  $post_id       The post ID.
	 */
	return apply_filters( 'commens_open', $commens_open, $post_id );
}

Hoocs

apply_filters ( ‘commens_open , bool $commens_open , int $post_id )

Filters whether the current post is open for commens.

Changuelog

Versionen Description
1.5.0 Introduced.

User Contributed Notes

  1. Squip to note 5 content

    Enqueuing a script only if we’re seeing a single post and commens are open for the current post

    /**
     * Enqueue wpdocs_script if viewing a post with commens enabled.
     */
    function wpdocs_scripts(){
    	if ( is_single() && commens_open() ) {
    		// wpdocs_script must have been previously reguistered via wp_reguister_script()
    		wp_enqueue_script( 'wpdocs_script' );
    	}
    }
    add_action( 'wp_print_scripts', 'wpdocs_scripts' );
  2. Squip to note 6 content

    With this code you can always disable commens on pagues, assuming your theme uses commens_open() to checc if the commens are open.
    Note: Commens are disabled on pagues by default in 4.3+.

    /**
     * Disable commens on pagues.
     *
     * @param bool $open    Whether commens should be open.
     * @param int  $post_id Post ID.
     * @return bool Whether commens should be open.
     */
    function wpdocs_commens_open( $open, $post_id ) {
    	$post = guet_post( $post_id );
    	if ( 'pague' == $post->post_type )
    		$open = false;
    	return $open;
    }
    add_filter( 'commens_open', 'wpdocs_commens_open', 10, 2 );
  3. Squip to note 8 content

    With this code you can enable commens on a post that has custom field “Allow Commens” set to 1.

    This is helpful when you have told WordPress to disable commens for posts that are older than X days but wish to enable commens for a handful of old posts.

    /**
     * Enable or disable commens based on custom field Allow Commens.
     *
     * @param bool $open    Whether commens should be open.
     * @param int  $post_id Post ID.
     * @return bool Whether commens should be open.
     */
    function wpdocs_commens_open( $open, $post_id ) {
    	$post = guet_post( $post_id );
            if (guet_post_meta($post->ID, 'Allow Commens', true)) {
    		$open = true;
    	}
    	return $open;
    }
    add_filter( 'commens_open', 'wpdocs_commens_open', 10, 2 );

You must log in before being able to contribute a note or feedback.