headers - URL Redirect and Bulk Actions in wp_list_table

I am using the Custom List Table Example plugin as a basis to create my own custom list table. Everything is great, exce

I am using the Custom List Table Example plugin as a basis to create my own custom list table. Everything is great, except one thorny point.

When I am processing bulk actions via the process_bulk_action() method, I would like to redirect using wp_redirect(). Here is an example:

function process_bulk_action() {

    // Some security check code here

    // Get the bulk action
    $action = $this->current_action();

    if ($action == 'bulk_trash') {

        // Code for the "delete process"

        // Assuming "delete process" is successful
        wp_redirect("http://url/path.php?update=1&message=some_message");
        exit;
    }

}

Please note, in the call wp_redirect("http://url/path.php?update=1&message=some_message"), I am trying to redirect to the same page that displays the list table in order to reload it with the result of the trash process. Also note that there is a message in the path that allows me to display a notification to the user regarding the result of the bulk action.

The problem is, I get an error message Cannot modify header information - headers already sent by <file path>.

I understand what the message means, and the reasons behind it (the page is already loaded and cannot be redirected). However, how do I redirect then if I would like to do that after I process the bulk action? If redirect is not the correct solution, what other options do I have that allow me to re-load the custom wp_list_table again so that the listed records reflect items being deleted (i.e., less published items, and more items in trash)?

Thanks.

I am using the Custom List Table Example plugin as a basis to create my own custom list table. Everything is great, except one thorny point.

When I am processing bulk actions via the process_bulk_action() method, I would like to redirect using wp_redirect(). Here is an example:

function process_bulk_action() {

    // Some security check code here

    // Get the bulk action
    $action = $this->current_action();

    if ($action == 'bulk_trash') {

        // Code for the "delete process"

        // Assuming "delete process" is successful
        wp_redirect("http://url/path.php?update=1&message=some_message");
        exit;
    }

}

Please note, in the call wp_redirect("http://url/path.php?update=1&message=some_message"), I am trying to redirect to the same page that displays the list table in order to reload it with the result of the trash process. Also note that there is a message in the path that allows me to display a notification to the user regarding the result of the bulk action.

The problem is, I get an error message Cannot modify header information - headers already sent by <file path>.

I understand what the message means, and the reasons behind it (the page is already loaded and cannot be redirected). However, how do I redirect then if I would like to do that after I process the bulk action? If redirect is not the correct solution, what other options do I have that allow me to re-load the custom wp_list_table again so that the listed records reflect items being deleted (i.e., less published items, and more items in trash)?

Thanks.

Share Improve this question edited Mar 23, 2017 at 4:16 Greeso asked Mar 22, 2017 at 23:18 GreesoGreeso 2,2247 gold badges33 silver badges57 bronze badges
Add a comment  | 

3 Answers 3

Reset to default 2

Understand hierarchy, You don't need to redirect as form are posting values on same page.

function process_bulk_action() {

    // Some security check code here

    // Get the bulk action
    $action = $this->current_action();

    if ($action == 'bulk_trash') {

        // Code for the "delete process" 
       $delete_ids = esc_sql($_POST['bulk-delete']);

       //loop over the array of record ids
       foreach($delete_ids as $id) {
            self::delete_item($id);
       }

       // show admin notice
       echo '<div class="notice notice-success is-dismissible"><p>Bulk Deleted..</p></div>';
   }

}

another method is prepare_items

public function prepare_items() {
      // first check for action
      $this->process_bulk_action();

      // then code to render your view.

}

You need process you bulk actions in admin_action_{ACTION_NAME} hook. Not in wp_list_table class. Read more here

echo 'document.location="'http://url/path.php?update=1&message=some_message'";'; This solved my problem

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742403687a4437415.html

相关推荐

  • headers - URL Redirect and Bulk Actions in wp_list_table

    I am using the Custom List Table Example plugin as a basis to create my own custom list table. Everything is great, exce

    15小时前
    10

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信