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:
/**
* 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.
try this three function in ” class My_Example_List_Table extends WP_List_Table ” class and modification as per your requirement
Here’s how I solved this:
myCheckboxes[]
Here is the relevant missing bottom portion that you can see was changed to match the new checkbox value: