【问题标题】:Spam post types in WordPressWordPress中的垃圾邮件类型
【发布时间】:2020-01-31 00:02:54
【问题描述】:

我正在尝试将操作“垃圾邮件”添加到我的帖子类型操作中,但在单击链接时我收到此错误消息“您关注的链接已过期。”。遵循一些说明,例如增加 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 );

提前感谢您的帮助。

【问题讨论】:

    标签: php wordpress custom-post-type


    【解决方案1】:

    该错误与您的 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 ); 
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-01-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-07-12
      • 2016-11-07
      • 1970-01-01
      相关资源
      最近更新 更多