I'm trying to do a delete action in custom plugin which uses a custom table and extends wp_list_table for showing the data. The problem is, whenever I try to delete the data I get cannot modify header information and I don't know how to fix it. The code:
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 = $_REQUEST['_wpnonce'];
if ( !wp_verify_nonce( $nonce, 'bsp_delete_student' ) ) {
die( 'Invalid security check!' );
}
else {
self::delete_student( absint( $_GET['student'] ) );
wp_redirect( esc_url( add_query_arg() ) );
exit;
}
}
// If the delete bulk action is triggered
if ( ( isset( $_POST['action'] ) && $_POST['action'] == 'delete' )
|| ( isset( $_POST['action2'] ) && $_POST['action2'] == 'delete' )
) {
$delete_ids = esc_sql( $_POST['delete'] );
// loop over the array of record IDs and delete them
foreach ($delete_ids as $id ) {
self::delete_student( $id );
}
wp_redirect( esc_url( add_query_arg() ) );
exit;
}
}
I then have a function for deleting:
function delete_student($id){
global $wpdb;
$wpdb->delete("{$wpdb->prefix}students",
[ 'students_id' => $id ],
[ '%d' ]
);
}
When I comment out the nonce check (which is not working, always gives me the "Invalid security check!") and try to delete a student I get the cannot modify header information and the selected row is not deleted. Any suggestions?
wp_redirect
call? Even a blank space at the end of a file? Any output of any kind will trigger PHP to send output headers, and once they're sent, they're sent, you can't undo it and send more. Since redirects work by sending headers, you can't have any output beforehand