函数文档

get_default_comment_status()

💡 云策文档标注

概述

get_default_comment_status() 函数用于获取指定文章类型的默认评论状态。它根据文章类型和评论类型返回 'open' 或 'closed' 状态,并可通过过滤器进行自定义。

关键要点

  • 函数返回字符串 'open' 或 'closed',表示默认评论状态。
  • 参数 $post_type 指定文章类型,默认值为 'post';$comment_type 指定评论类型,默认值为 'comment'。
  • 对于 'page' 文章类型,默认状态为 'closed';其他文章类型根据 post_type_supports() 检查支持情况。
  • 支持通过 apply_filters('get_default_comment_status', ...) 钩子过滤默认状态。
  • 函数内部处理 'pingback' 和 'trackback' 评论类型,使用不同的支持和选项键。

代码示例

function get_default_comment_status( $post_type = 'post', $comment_type = 'comment' ) {
    switch ( $comment_type ) {
        case 'pingback':
        case 'trackback':
            $supports = 'trackbacks';
            $option   = 'ping';
            break;
        default:
            $supports = 'comments';
            $option   = 'comment';
            break;
    }

    // Set the status.
    if ( 'page' === $post_type ) {
        $status = 'closed';
    } elseif ( post_type_supports( $post_type, $supports ) ) {
        $status = get_option( "default_{$option}_status" );
    } else {
        $status = 'closed';
    }

    /**
     * Filters the default comment status for the given post type.
     *
     * @since 4.3.0
     *
     * @param string $status       Default status for the given post type,
     *                             either 'open' or 'closed'.
     * @param string $post_type    Post type. Default is `post`.
     * @param string $comment_type Type of comment. Default is `comment`.
     */
    return apply_filters( 'get_default_comment_status', $status, $post_type, $comment_type );
}

注意事项

  • 函数自 WordPress 4.3.0 版本引入,使用时需确保版本兼容性。
  • 默认评论状态可通过 WordPress 后台设置中的“讨论”选项进行配置,影响 get_option() 返回值。
  • 钩子 apply_filters('get_default_comment_status', ...) 允许开发者动态修改默认状态,需注意参数顺序和类型。

📄 原文内容

Gets the default comment status for a post type.

Parameters

$post_typestringoptional
Post type. Default 'post'.
$comment_typestringoptional
Comment type. Default 'comment'.

Return

string Either 'open' or 'closed'.

Source

function get_default_comment_status( $post_type = 'post', $comment_type = 'comment' ) {
	switch ( $comment_type ) {
		case 'pingback':
		case 'trackback':
			$supports = 'trackbacks';
			$option   = 'ping';
			break;
		default:
			$supports = 'comments';
			$option   = 'comment';
			break;
	}

	// Set the status.
	if ( 'page' === $post_type ) {
		$status = 'closed';
	} elseif ( post_type_supports( $post_type, $supports ) ) {
		$status = get_option( "default_{$option}_status" );
	} else {
		$status = 'closed';
	}

	/**
	 * Filters the default comment status for the given post type.
	 *
	 * @since 4.3.0
	 *
	 * @param string $status       Default status for the given post type,
	 *                             either 'open' or 'closed'.
	 * @param string $post_type    Post type. Default is `post`.
	 * @param string $comment_type Type of comment. Default is `comment`.
	 */
	return apply_filters( 'get_default_comment_status', $status, $post_type, $comment_type );
}

Hooks

apply_filters( ‘get_default_comment_status’, string $status, string $post_type, string $comment_type )

Filters the default comment status for the given post type.

Changelog

Version Description
4.3.0 Introduced.