While the actual deletion of data from our custom table can be done with a single call to run the DELETE SQL command, we first need the user to indicate which entries need to be removed. This selection interface can be easily added to the existing bug listing created in an earlier recipe.
This recipe starts in familiar territory with the creation of a standard HTML form to surround the original bug listing. In addition to the bug list, the form also includes a hidden field to indicate the name of the action to be called when the user submits the form, along with a nonce field to ensure that access to the deletion process is secure.
With this initial code in place, the next section of the recipe modifies the original table listing to add a checkbox at the front of every row. As can be seen in the code, the name property of the checkbox is a bit different than regular HTML syntax, ending with two square parentheses. This syntax, used in conjunction with each item's bug_id, results in the creation of an array of checked items and ID numbers that are sent to the form processing function on submission.
The last change that is done in the bug listing display code is to add a deletion button and to close the form.
To associate a callback with the newly created form, the next addition made by the recipe is a call to add_action to associate the admin_post_<actionname> variable action name with the delete_ch8bt_bug function.
When called, the bug deletion function, like most other submission processing code that we have created before, first starts with a few verifications to make sure that the user has appropriate permissions and that the hidden security fields that were placed in the form are present. When both of these formalities are confirmed, the code goes on to check for the presence of a bug array and proceeds to cycle through all the entries if one was found. In that loop, we get access to the global wpdb class and we can use it to build and execute SQL queries that delete a single database row at a time using the bug_id numbers that were submitted.
As an added security measure, notice the use of the intval function in front of the $bug_to_delete variable to make sure that no one is trying to get external commands to be processed in an attempt to corrupt or hijack the database.