WordPress 通过在管理中单击自定义 link 更新自定义 post 状态

WordPress Update custom post status by clicking custom link in admin

我有一个网站,我从用户那里得到 posts,那些 posts 作为自定义 post 保存在 WP 中,状态待定, 管理员在该自定义 post 类型页面中有自定义 link拒绝 批准应将 post 状态更改为 Public 拒绝 应将 post 状态更改为 trash/del

任何人都可以通过单击这些自定义 links

告诉我可以 运行 从后端更改 post 状态的任何挂钩

批准代码 |如果有人想看,请拒绝按钮

add_filter( 'manage_dream_posts_columns', 'smashing_filter_posts_columns' );
function smashing_filter_posts_columns( $columns ) {
  $columns['decision'] = __( 'Decision Pending', 'rima' );
  return $columns;
}
add_action( 'manage_dream_posts_custom_column', 'smashing_dream_column', 10, 2);
function smashing_dream_column( $column, $post_id ) {

  if ( 'decision' === $column ) {
    if (get_post_status ( $post_id ) == 'pending') {
        echo '<div class="decision"><a href="#" data-userID="'.$post_id.'" class="dapprove" onclick="decision()">Approve</a><a href="#" class="dreject">Reject</a></div>';
    }
    else {
        echo '<div class="decision"><span class="dapprove">Approved</span></div>';
    }
  }
}

您可以这样做 - 它有点老套,使用 jQuery,但使用 Ajax 方法可以快速解决您的问题,因此它可以在您的管理屏幕上实时运行。您可以 approve/reject 多个帖子而无需重新加载任何页面,包括一些颜色反馈,让您知道发生了什么。

您需要添加 2 个新操作:

add_action( 'wp_ajax_set_post_status', 'set_post_status_ajax_handler' );
add_action( 'admin_footer', 'set_post_status_js' );

然后添加这些功能:

function set_post_status_js()
{
  $nonce = wp_create_nonce('set_post_status');
  $ajax_url = admin_url('admin-ajax.php'); ?>

  <script type="text/javascript">
    (function($){
      $(document).ready(function(){
        $('.decision a').click(function(event){
          event.preventDefault();
          $.post( "<?= $ajax_url; ?>", {
            nonce: "<?= $nonce; ?>",
            action: 'set_post_status',
            post_id: $(this).data('post_id'),
            status: $(this).data('status'),
          }, function(data){
            if (data.ok) {
              var postStateLabel = (data.status === 'publish') ? '<span style="color: #009900;">Approved</span>' : '<span style="color: #990000;">Rejected</span>';

              $('#post-' + data.id)
                .css('background', data.status === 'publish' ? '#EEFFEE' : '#FFEEEE')
                .find('.post-state').html( postStateLabel );
            }
          });
        });
      });
    })(jQuery)
  </script>

  <?php
}

function set_post_status_ajax_handler()
{
  $nonce = $_POST['nonce'];

  if ( ! wp_verify_nonce( $nonce, 'set_post_status' ) )
    die ( 'Not permitted');

  // Extract the vars from the Ajax request
  $post_id = $_POST['post_id'];
  $status = $_POST['status'];

  // Now update the relevant post
  $post_id = wp_update_post([
    'ID' => $post_id,
    'post_status' => $status,
  ], true);

  // make sure it all went OK
  if (is_wp_error($post_id))
  {
    $response = [
      'ok' => false,
    ];
  } else 
  {
    $response = [
      'ok'      => true,
      'id'      => $post_id,
      'status'  => $status,
    ];
  }

  // Return the response
  wp_send_json( $response );
}

最后,您需要将批准/拒绝链接更改为 HTML:

echo '
<div class="decision">
  <a href="#" data-post_id="' . $post_id . '" data-status="publish" class="dapprove">Approve</a>
  <a href="#" data-post_id="' . $post_id . '" data-status="trash" class="dreject">Reject</a>
</div>';

希望这会让您满意。干杯。