wp_transition_comment_status

函式
wp_transition_comment_status ( $new_status, $old_status, $comment )
引數
  • (string) $new_status New comment status.
    Required:
  • (string) $old_status Previous comment status.
    Required:
  • (WP_Comment) $comment Comment object.
    Required:
定義位置
相關方法
wp_transition_post_statuswp_set_comment_status_transition_post_statuswp_get_comment_statuswp_trash_post_comments
引入
2.7.0
棄用
-

wp_transition_comment_status: 當一個評論的狀態發生變化時,例如從待定到批准,這個函式會觸發行動。

當評論狀態轉換髮生時,呼叫鉤子。

呼叫評論狀態轉換的鉤子。如果新的評論狀態與之前的評論狀態不一樣,那麼將執行兩個鉤子,第一個是{@see ‘transition_comment_status’},包含新狀態、舊狀態和評論資料。接下來呼叫的動作是{@see ‘comment_$old_status_to_$new_status’}。它有評論資料。

最後一個動作將執行評論狀態是否相同。
這個動作被命名為{@see ‘comment_$new_status_$comment-> comment_type’}。

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
function wp_transition_comment_status( $new_status, $old_status, $comment ) {
/*
* Translate raw statuses to human-readable formats for the hooks.
* This is not a complete list of comment status, it's only the ones
* that need to be renamed.
*/
$comment_statuses = array(
0 => 'unapproved',
'hold' => 'unapproved', // wp_set_comment_status() uses "hold".
1 => 'approved',
'approve' => 'approved', // wp_set_comment_status() uses "approve".
);
if ( isset( $comment_statuses[ $new_status ] ) ) {
$new_status = $comment_statuses[ $new_status ];
}
if ( isset( $comment_statuses[ $old_status ] ) ) {
$old_status = $comment_statuses[ $old_status ];
}
// Call the hooks.
if ( $new_status != $old_status ) {
/**
* Fires when the comment status is in transition.
*
* @since 2.7.0
*
* @param int|string $new_status The new comment status.
* @param int|string $old_status The old comment status.
* @param WP_Comment $comment Comment object.
*/
do_action( 'transition_comment_status', $new_status, $old_status, $comment );
/**
* Fires when the comment status is in transition from one specific status to another.
*
* The dynamic portions of the hook name, `$old_status`, and `$new_status`,
* refer to the old and new comment statuses, respectively.
*
* Possible hook names include:
*
* - `comment_unapproved_to_approved`
* - `comment_spam_to_approved`
* - `comment_approved_to_unapproved`
* - `comment_spam_to_unapproved`
* - `comment_unapproved_to_spam`
* - `comment_approved_to_spam`
*
* @since 2.7.0
*
* @param WP_Comment $comment Comment object.
*/
do_action( "comment_{$old_status}_to_{$new_status}", $comment );
}
/**
* Fires when the status of a specific comment type is in transition.
*
* The dynamic portions of the hook name, `$new_status`, and `$comment->comment_type`,
* refer to the new comment status, and the type of comment, respectively.
*
* Typical comment types include 'comment', 'pingback', or 'trackback'.
*
* Possible hook names include:
*
* - `comment_approved_comment`
* - `comment_approved_pingback`
* - `comment_approved_trackback`
* - `comment_unapproved_comment`
* - `comment_unapproved_pingback`
* - `comment_unapproved_trackback`
* - `comment_spam_comment`
* - `comment_spam_pingback`
* - `comment_spam_trackback`
*
* @since 2.7.0
*
* @param string $comment_ID The comment ID as a numeric string.
* @param WP_Comment $comment Comment object.
*/
do_action( "comment_{$new_status}_{$comment->comment_type}", $comment->comment_ID, $comment );
}
function wp_transition_comment_status( $new_status, $old_status, $comment ) { /* * Translate raw statuses to human-readable formats for the hooks. * This is not a complete list of comment status, it's only the ones * that need to be renamed. */ $comment_statuses = array( 0 => 'unapproved', 'hold' => 'unapproved', // wp_set_comment_status() uses "hold". 1 => 'approved', 'approve' => 'approved', // wp_set_comment_status() uses "approve". ); if ( isset( $comment_statuses[ $new_status ] ) ) { $new_status = $comment_statuses[ $new_status ]; } if ( isset( $comment_statuses[ $old_status ] ) ) { $old_status = $comment_statuses[ $old_status ]; } // Call the hooks. if ( $new_status != $old_status ) { /** * Fires when the comment status is in transition. * * @since 2.7.0 * * @param int|string $new_status The new comment status. * @param int|string $old_status The old comment status. * @param WP_Comment $comment Comment object. */ do_action( 'transition_comment_status', $new_status, $old_status, $comment ); /** * Fires when the comment status is in transition from one specific status to another. * * The dynamic portions of the hook name, `$old_status`, and `$new_status`, * refer to the old and new comment statuses, respectively. * * Possible hook names include: * * - `comment_unapproved_to_approved` * - `comment_spam_to_approved` * - `comment_approved_to_unapproved` * - `comment_spam_to_unapproved` * - `comment_unapproved_to_spam` * - `comment_approved_to_spam` * * @since 2.7.0 * * @param WP_Comment $comment Comment object. */ do_action( "comment_{$old_status}_to_{$new_status}", $comment ); } /** * Fires when the status of a specific comment type is in transition. * * The dynamic portions of the hook name, `$new_status`, and `$comment->comment_type`, * refer to the new comment status, and the type of comment, respectively. * * Typical comment types include 'comment', 'pingback', or 'trackback'. * * Possible hook names include: * * - `comment_approved_comment` * - `comment_approved_pingback` * - `comment_approved_trackback` * - `comment_unapproved_comment` * - `comment_unapproved_pingback` * - `comment_unapproved_trackback` * - `comment_spam_comment` * - `comment_spam_pingback` * - `comment_spam_trackback` * * @since 2.7.0 * * @param string $comment_ID The comment ID as a numeric string. * @param WP_Comment $comment Comment object. */ do_action( "comment_{$new_status}_{$comment->comment_type}", $comment->comment_ID, $comment ); }
function wp_transition_comment_status( $new_status, $old_status, $comment ) {
	/*
	 * Translate raw statuses to human-readable formats for the hooks.
	 * This is not a complete list of comment status, it's only the ones
	 * that need to be renamed.
	 */
	$comment_statuses = array(
		0         => 'unapproved',
		'hold'    => 'unapproved', // wp_set_comment_status() uses "hold".
		1         => 'approved',
		'approve' => 'approved',   // wp_set_comment_status() uses "approve".
	);
	if ( isset( $comment_statuses[ $new_status ] ) ) {
		$new_status = $comment_statuses[ $new_status ];
	}
	if ( isset( $comment_statuses[ $old_status ] ) ) {
		$old_status = $comment_statuses[ $old_status ];
	}

	// Call the hooks.
	if ( $new_status != $old_status ) {
		/**
		 * Fires when the comment status is in transition.
		 *
		 * @since 2.7.0
		 *
		 * @param int|string $new_status The new comment status.
		 * @param int|string $old_status The old comment status.
		 * @param WP_Comment $comment    Comment object.
		 */
		do_action( 'transition_comment_status', $new_status, $old_status, $comment );
		/**
		 * Fires when the comment status is in transition from one specific status to another.
		 *
		 * The dynamic portions of the hook name, `$old_status`, and `$new_status`,
		 * refer to the old and new comment statuses, respectively.
		 *
		 * Possible hook names include:
		 *
		 *  - `comment_unapproved_to_approved`
		 *  - `comment_spam_to_approved`
		 *  - `comment_approved_to_unapproved`
		 *  - `comment_spam_to_unapproved`
		 *  - `comment_unapproved_to_spam`
		 *  - `comment_approved_to_spam`
		 *
		 * @since 2.7.0
		 *
		 * @param WP_Comment $comment Comment object.
		 */
		do_action( "comment_{$old_status}_to_{$new_status}", $comment );
	}
	/**
	 * Fires when the status of a specific comment type is in transition.
	 *
	 * The dynamic portions of the hook name, `$new_status`, and `$comment->comment_type`,
	 * refer to the new comment status, and the type of comment, respectively.
	 *
	 * Typical comment types include 'comment', 'pingback', or 'trackback'.
	 *
	 * Possible hook names include:
	 *
	 *  - `comment_approved_comment`
	 *  - `comment_approved_pingback`
	 *  - `comment_approved_trackback`
	 *  - `comment_unapproved_comment`
	 *  - `comment_unapproved_pingback`
	 *  - `comment_unapproved_trackback`
	 *  - `comment_spam_comment`
	 *  - `comment_spam_pingback`
	 *  - `comment_spam_trackback`
	 *
	 * @since 2.7.0
	 *
	 * @param string     $comment_ID The comment ID as a numeric string.
	 * @param WP_Comment $comment    Comment object.
	 */
	do_action( "comment_{$new_status}_{$comment->comment_type}", $comment->comment_ID, $comment );
}

常見問題

FAQs
檢視更多 >