how do I render a bulk edit checkbox on wp_list_table

I’m trying to use wp_list_table by making an extended class. I have two actions to run through on the bulk-update functionality provided. However I don’t quite know how to write the proper case switch statement to set the resulting checkbox to apply for either array actions based on the action taken…(see I know what I need to do, but I don’t quite know how to do it).

This is the smallest bit of relatable code I have to work with:

Read More
  /**
   * Render the bulk edit checkbox
   *
   * @param array $item
   *
   * @return string
   */
  function column_cb( $item ) {
    return sprintf(
      '<input type="checkbox" name="bulk-reset[]" value="%s" />', $item['id']

    );
  }


  /**
   * Method for name column
   *
   * @param array $item an array of DB data
   *
   * @return string
   */
  function column_name( $item ) {

    $delete_nonce = wp_create_nonce( 'sp_delete_customer' );
    $reset_nonce = wp_create_nonce( 'sp_reset_payouts' );

    $title = '<strong>' . $item['name'] . '</strong>';

    $actions = [
      'delete' => sprintf( '<a href="?page=%s&action=%s&customer=%s&_wpnonce=%s">Delete</a>', esc_attr( $_REQUEST['page'] ), 'delete', absint( $item['id'] ), $delete_nonce ),

      'reset' => sprintf( '<a href="?page=%s&action=%s&customer=%s&_wpnonce=%s">Reset</a>', esc_attr( $_REQUEST['page'] ), 'reset', absint( $item['id'] ), $reset_nonce )

    ];

    return $title . $this->row_actions( $actions );
  }

  /**
   * Returns an associative array containing the bulk action
   *
   * @return array
   */
  public function get_bulk_actions() {
    $actions = array(
      'bulk-delete' => 'Delete',
      'bulk-reset' => 'Reset'
    );

    return $actions;
  }

Reviewing this one last time, I think I need a function that is called on the name property as an escaped php echo right? Then that function should return either bulk-reset[] or bulk-delete[] based on $_post['delete-*'] right???

Since mulling this over, I’ve tried to pen together the solution, but for whatever reason that action_type function I made does not get called:

  /**
   * Render the bulk edit checkbox
   *
   * @param array $item
   *
   * @return string
   */
  function column_cb( $item ) {

    global $action_type;

    return sprintf(
      '<input type="checkbox" name="'.$action_type.'" value="%s" />', $item['id']

    );
  }



  /**
   * Pick the Checkbox Value based on post Value
   *
   * 
   * @return string
   *
   */

  public function action_type() {

     if ( ( isset( $_POST['action'] ) && $_POST['action'] == 'bulk-delete' )
         || ( isset( $_POST['action2'] ) && $_POST['action2'] == 'bulk-delete' )
    ) {
      $output = "bulk-delete[]";

     } else {
      $output = "bulk-reset[]";
     }
     return $output;

  }

Edit 2: realizing that won’t work because this sets the value post and I needed it set before page load…I guess this means I need to have the array name be generic and the post action handles how to deal with the data… maybe for simplicity sake I should set the edit action to the same bulk-delete data set as its ultimately the same set of checkboxes.

Edit 3: I can’t do what I suggest at the end of edit2 because the portion of code to run the actions depends on the array name of the post and that array name is set on page load. I don’t quite know what to do.

Related posts

Leave a Reply

2 comments

  1. try this three function in ” class My_Example_List_Table extends WP_List_Table ” class and modification as per your requirement

    function column_id($item){
      $actions = array(
              /*  'edit'      => sprintf('<a href="?page=%s&action=%s&book=%s">Edit</a>',$_REQUEST['page'],'edit',$item['ID']),*/
         'delete' => sprintf('<a href="?page=%s&action=%s&id=%s">Delete</a>',$_REQUEST['page'],'delete',$item['id']),
            );
      return sprintf('%1$s %2$s', $item['id'], $this->row_actions($actions) );
    }
    
    
    
    
    function get_bulk_actions() {
    
      $actions = array(
        'delete'    => 'Delete'
      );
      return $actions;
    }
    
    function process_bulk_action(){
    
        global $wpdb;
        $table_name = $wpdb->prefix."tablename"; 
    
            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 $table_name WHERE id IN($ids)");
                }
    
            }
     }
    
  2. Here’s how I solved this:

    1. Change (or not ????  ) the checkbox array value to something generic. I chose myCheckboxes[]
    2. this part is not shown in the answer above but the portion of the code executing the update or delete queries was the point where the data was set…I saw that changing the value there allows my checkbox column to be applied to all actions. (I’m planning to refactor the code further once I solve a related issue so this isn’t the entire code snippet (ie. this is NOT a drop in copy/paste replacement, just context for how I solved this)

    Here is the relevant missing bottom portion that you can see was changed to match the new checkbox value:

    // // If the delete bulk action is triggered
    if ( ( isset( $_POST['action'] ) && $_POST['action'] == 'bulk-delete' )
         || ( isset( $_POST['action2'] ) && $_POST['action2'] == 'bulk-delete' )
    ) {
    
      $delete_ids = esc_sql( $_POST['my_CheckBoxes'] );
    
      // loop over the array of record IDs and delete them
      foreach ( $delete_ids as $id ) {
        self::delete_customer( $id );
      }
      wp_redirect( esc_url( add_query_arg() ) );
      exit;
    }