该错误与您的 Wordpress Nonce 验证有关,而不是您的 PHP 设置。此外,在您的情况下,您应该调用post.php 页面而不是edit.php 页面,并且必须声明您的自定义发布操作请求(垃圾邮件)以使用add_action( "post_action_{$action}", 'my_custom_action', 10, 1 ); 处理请求,请参阅文档:https://developer.wordpress.org/reference/hooks/post_action_action/
在 my_custom_action() 函数上,您应该使用 Wordpress check_admin_referer() 函数验证您在 post_row_actions 过滤器上声明的随机数。
所以,让我们总结一下:
首先将edit.php更新为post.php:
function wpc_remove_add_row_actions( $actions, $post ){
if( $post->post_type === 'cpost' ){
$url = admin_url( 'post.php?post=' . $post->ID );
$spam_link = wp_nonce_url(add_query_arg( array( 'action' => 'spam' ), $url ), 'cpost');
$actions['spam'] = '<a href="'.$spam_link.'">Spam</a>';
}
return $actions;
}
add_filter( 'post_row_actions', 'wpc_remove_add_row_actions', 10, 2 );
根据 Wordpress 文档,wp_nonce_url() 的第二个参数是“Nonce 操作名称”。请参阅文档:https://developer.wordpress.org/reference/functions/wp_nonce_url/
wp_nonce_url(add_query_arg(array('action' => 'spam'), $url),
'cpost');
所以,你的 nonce 名字是 cpost
现在您只需要设置您的自定义操作处理程序:
// define the post_action_<action> callback
function my_custom_action_spam( $post_id ) {
check_admin_referer('cpost'); //Your nonce name validation
// make action magic happen here...
};
// add the action
add_action( "post_action_spam", 'my_custom_action_spam', 10, 1 );