WordPress 中的垃圾邮件 post 类型

Spam post types in WordPress

我正在尝试将操作 'spam' 添加到我的 post 类型操作中,但是当单击 link 时,我收到此错误 'The link you followed has expired.'。按照一些说明进行操作,例如增加 max_execution_time nothing 它按预期工作。下面是代码。

function wpc_remove_add_row_actions( $actions, $post ){
   if( $post->post_type === 'cpost' ){ 
      $url = admin_url( 'edit.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 Nonce 验证,而不是您的 PHP 设置。此外,在您的情况下,您应该调用 post.php 页面而不是 edit.php 页面,并且必须声明您的自定义 post 操作请求(垃圾邮件)以使用 [=14= 处理请求],请参阅文档: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 action name'。请参阅文档:https://developer.wordpress.org/reference/functions/wp_nonce_url/

wp_nonce_url(add_query_arg( array( 'action' => 'spam' ), $url ), 'cpost');

因此,您的随机数名称是 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 );