3

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.

3 Answers 3

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.

}
1

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

1
  • don't work with action2 from bottom of list table :( Commented Aug 30, 2018 at 13:57
-3

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

1
  • 1
    Welcome to WPSE! Please do not just dump code but explain how and what it does so people can actually learn instead of just Copy&Paste code.
    – kraftner
    Commented Jul 5, 2017 at 11:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.