我正在尝试将操作“垃圾邮件”添加到我的帖子类型操作中,但是当单击链接时,出现此错误“您关注的链接已过期。”。遵循了一些指示,例如增加max_execution_time使其无法按预期工作。在下面您有代码。
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
页面,并且必须声明您的自定义后期处理请求(垃圾邮件)以使用add_action( "post_action_{$action}", 'my_custom_action', 10, 1 );
处理请求,请参阅文档:https://developer.wordpress.org/reference/hooks/post_action_action/
在my_custom_action()
函数上,您应该使用Wordpress post_row_actions
函数验证在check_admin_referer()
过滤器上声明的随机数。
所以,让我们把所有的东西都包装起来:
首先将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 docs,wp_nonce_url()
的第二个参数是'Nonce操作名称'。参见文档: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 );