Shperblim Aliu
Shperblim Aliu

Reputation: 3

wp_list_table bulk action delete button is not working - no submit

I need to perform bulk actions and show a success message to the user based on the action. But I couldn't get it working.

Here is my code:

function get_bulk_actions() {
  $actions = array(
    'delete' => 'Delete'
  );

  return $actions;
}

/**
* [OPTIONAL] This method processes bulk actions
* it can be outside of class
* it can not use wp_redirect coz there is output already
* in this example we are processing delete action
* message about successful deletion will be shown on page in next part
*/

function process_bulk_action() {
  
  global $wpdb;
  $table_name = $wpdb->prefix . 'ip_poi_map_list'; // do not forget about tables prefix

  if ('delete' === $this->current_action()) {
    
    $ids = isset($_REQUEST['id']) ? $_REQUEST['id'] : array();
    
    if (is_array($ids)){ $ids = implode(',', $ids); }
      
    if (!empty($ids)) {
      $wpdb->query("DELETE FROM wp_ip_poi_map_list WHERE id IN('%$ids%')");
    }
  }
}

there id image

Upvotes: 0

Views: 1810

Answers (1)

Box
Box

Reputation: 2502

You may want to refer to https://github.com/w3guy/WP_List_Table-Class-Plugin-Example. It has a few minor bugs, but the process_bulk_action() function works.

public function process_bulk_action() {
    // Detect when a bulk action is being triggered...
    if ( 'delete' === $this->current_action() ) {

        // In our file that handles the request, verify the nonce.
        $nonce = esc_attr( $_REQUEST['_wpnonce'] );

        if ( ! wp_verify_nonce( $nonce, 'sp_delete_customer' ) ) {
            die( 'Go get a life script kiddies' );
        } else {
            self::delete_customer( absint( $_GET['customer'] ) );

                    // esc_url_raw() is used to prevent converting ampersand in url to "#038;"
                    // add_query_arg() return the current url
                    wp_redirect( esc_url_raw( add_query_arg() ) );
            exit;
        }
    }

    // If the delete bulk action is triggered
    if ( ( isset( $_POST['action'] ) && 'bulk-delete' == $_POST['action'] )
         || ( isset( $_POST['action2'] ) && 'bulk-delete' == $_POST['action2'] )
    ) {
        $delete_ids = esc_sql( $_POST['bulk-delete'] );

        // loop over the array of record IDs and delete them
        foreach ( $delete_ids as $id ) {
            self::delete_customer( $id );
        }

        // esc_url_raw() is used to prevent converting ampersand in url to "#038;"
        // add_query_arg() return the current url
        wp_redirect( esc_url_raw( add_query_arg() ) );
        exit;
    }
}

More test data was generated by https://www.mockaroo.com/.

enter image description here

enter image description here

Upvotes: 1

Related Questions