这里有一个使用PHP而不是JS方法的解决方案。(我无法用JS取得任何进展)。
这段代码将用一个新的操作替换WP的垃圾操作,该操作只会垃圾掉评论并重新加载页面。
/**
* Replace the Trash action in the comment list with a link that will trash the comment and reload the page.
*
* @see https://wordpress.stackexchange.com/questions/268236/edit-comments-php-comment-row-actions-ajax-problem
*
* @param array $actions
* @param WP_Comment $comment
*
* @return array Comment actions
*/
function wpse_comment_row_actions( $actions, $comment ) {
// Don\'t do anything for comments that are already in the trash or marked as spam.
// You may want to customize this behavior.
if (
\'trash\' === $comment->comment_approved ||
\'spam\' === $comment->comment_approved ) {
return $actions;
}
// Remove WP\'s Trash action.
if ( isset( $actions[\'trash\'] ) ) {
unset( $actions[\'trash\'] );
}
$screen = get_current_screen();
$args = [
\'comment_id\' => $comment->comment_ID,
\'action\' => \'trash_comment_and_reload\',
\'_wpnonce\' => wp_create_nonce( \'do-comment-action-and-reload\' ),
\'referer\' => $screen->parent_file,
];
// Add args so that we redirect to the same tab that the user was on.
if ( ! empty( $_REQUEST[\'comment_status\'] ) ) {
$args[\'comment_status\'] = $_REQUEST[\'comment_status\'];
}
if ( ! empty( $_REQUEST[\'user_id\'] ) ) {
$args[\'user_id\'] = $_REQUEST[\'user_id\'];
}
$link = esc_url(
add_query_arg(
$args,
admin_url( \'admin-ajax.php\' )
)
);
// Add our own Trash action.
$actions[\'trash_and_reload\'] = sprintf(
\'<a href="%s" style="color:#b32d2e" class="delete">Trash</a>\',
$link
);
return $actions;
}
add_filter( \'comment_row_actions\', \'wpse_comment_row_actions\', 10, 2 );
/**
* Trash comment and reload the page.
*/
function wpse_trash_comment_and_reload() {
if ( wp_verify_nonce( $_REQUEST[\'_wpnonce\'], \'do-comment-action-and-reload\' ) ) {
wp_trash_comment( $_REQUEST[\'comment_id\'] );
$args = [];
if ( ! empty( $_REQUEST[\'comment_status\'] ) ) {
$args[\'comment_status\'] = $_REQUEST[\'comment_status\'];
}
if ( ! empty( $_REQUEST[\'user_id\'] ) ) {
$args[\'user_id\'] = $_REQUEST[\'user_id\'];
}
wp_safe_redirect(
add_query_arg(
$args,
admin_url( $_REQUEST[\'referer\'] )
)
);
}
exit;
}
add_action( \'wp_ajax_trash_comment_and_reload\', \'wpse_trash_comment_and_reload\' );