wp_ajax_get_comments

函式
wp_ajax_get_comments ( $action )
引數
  • (string) $action Action to perform.
    Required:
定義位置
相關方法
wp_ajax_edit_commentwp_ajax_delete_commentwp_ajax_replyto_commentwp_ajax_get_community_eventswp_ajax_dim_comment
引入
3.1.0
棄用
-

wp_ajax_get_comments函式是一個WordPress的AJAX動作,它為一個文章檢索評論並以HTML格式返回: 這個函式通常用於以動態方式顯示評論,而不需要重新載入頁面: 該函式首先檢查當前使用者是否有檢視評論的必要許可權,然後使用get_comments函式檢索評論。檢索到的評論會被格式化為HTML並在AJAX請求的響應中返回。

獲取評論的Ajax處理程式。

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
function wp_ajax_get_comments( $action ) {
global $post_id;
if ( empty( $action ) ) {
$action = 'get-comments';
}
check_ajax_referer( $action );
if ( empty( $post_id ) && ! empty( $_REQUEST['p'] ) ) {
$id = absint( $_REQUEST['p'] );
if ( ! empty( $id ) ) {
$post_id = $id;
}
}
if ( empty( $post_id ) ) {
wp_die( -1 );
}
$wp_list_table = _get_list_table( 'WP_Post_Comments_List_Table', array( 'screen' => 'edit-comments' ) );
if ( ! current_user_can( 'edit_post', $post_id ) ) {
wp_die( -1 );
}
$wp_list_table->prepare_items();
if ( ! $wp_list_table->has_items() ) {
wp_die( 1 );
}
$x = new WP_Ajax_Response();
ob_start();
foreach ( $wp_list_table->items as $comment ) {
if ( ! current_user_can( 'edit_comment', $comment->comment_ID ) && 0 === $comment->comment_approved ) {
continue;
}
get_comment( $comment );
$wp_list_table->single_row( $comment );
}
$comment_list_item = ob_get_clean();
$x->add(
array(
'what' => 'comments',
'data' => $comment_list_item,
)
);
$x->send();
}
function wp_ajax_get_comments( $action ) { global $post_id; if ( empty( $action ) ) { $action = 'get-comments'; } check_ajax_referer( $action ); if ( empty( $post_id ) && ! empty( $_REQUEST['p'] ) ) { $id = absint( $_REQUEST['p'] ); if ( ! empty( $id ) ) { $post_id = $id; } } if ( empty( $post_id ) ) { wp_die( -1 ); } $wp_list_table = _get_list_table( 'WP_Post_Comments_List_Table', array( 'screen' => 'edit-comments' ) ); if ( ! current_user_can( 'edit_post', $post_id ) ) { wp_die( -1 ); } $wp_list_table->prepare_items(); if ( ! $wp_list_table->has_items() ) { wp_die( 1 ); } $x = new WP_Ajax_Response(); ob_start(); foreach ( $wp_list_table->items as $comment ) { if ( ! current_user_can( 'edit_comment', $comment->comment_ID ) && 0 === $comment->comment_approved ) { continue; } get_comment( $comment ); $wp_list_table->single_row( $comment ); } $comment_list_item = ob_get_clean(); $x->add( array( 'what' => 'comments', 'data' => $comment_list_item, ) ); $x->send(); }
function wp_ajax_get_comments( $action ) {
	global $post_id;

	if ( empty( $action ) ) {
		$action = 'get-comments';
	}

	check_ajax_referer( $action );

	if ( empty( $post_id ) && ! empty( $_REQUEST['p'] ) ) {
		$id = absint( $_REQUEST['p'] );
		if ( ! empty( $id ) ) {
			$post_id = $id;
		}
	}

	if ( empty( $post_id ) ) {
		wp_die( -1 );
	}

	$wp_list_table = _get_list_table( 'WP_Post_Comments_List_Table', array( 'screen' => 'edit-comments' ) );

	if ( ! current_user_can( 'edit_post', $post_id ) ) {
		wp_die( -1 );
	}

	$wp_list_table->prepare_items();

	if ( ! $wp_list_table->has_items() ) {
		wp_die( 1 );
	}

	$x = new WP_Ajax_Response();

	ob_start();
	foreach ( $wp_list_table->items as $comment ) {
		if ( ! current_user_can( 'edit_comment', $comment->comment_ID ) && 0 === $comment->comment_approved ) {
			continue;
		}
		get_comment( $comment );
		$wp_list_table->single_row( $comment );
	}
	$comment_list_item = ob_get_clean();

	$x->add(
		array(
			'what' => 'comments',
			'data' => $comment_list_item,
		)
	);

	$x->send();
}

常見問題

FAQs
檢視更多 >