- Navigate to the WordPress plugin directory of your development installation.
- Navigate to the ch8-bug-tracker directory and edit ch8-bug-tracker.php.
- Find the ch8bt_config_page function and locate the Manage Bug Entries h3 header in its content.
- Insert the following highlighted lines of code right after the header to create a form:
<h3>Manage Bug Entries</h3>
<form method="post"
action="<?php echo admin_url( 'admin-post.php' ); ?>">
<input type="hidden" name="action" value="delete_ch8bt_bug" />
<!-- Adding security through hidden referrer field -->
<?php wp_nonce_field( 'ch8bt_deletion' ); ?>
- A few lines down, add an empty column in the table header, before the ID field, as highlighted in the following line of code:
<thead><tr><th style="width: 50px"></th>
<th style='width: 80px'>ID</th>
- Within the main bug list display loop, insert the following highlighted code segments to add a checkbox in front of each item:
echo '<tr style="background: #FFF">';
echo '<td><input type="checkbox" name="bugs[]" value="';
echo intval( $bug_item['bug_id'] ) . '" /></td>';
echo '<td>' . $bug_item['bug_id'] . '</td>';
- A few lines down, change the value of the colspan table row parameter from 3 to 4:
echo '<td colspan="4">No Bug Found</td></tr>';
- Append the following highlighted lines of code after the table close tag to display a deletion button and terminate the form section:
</table><br />
<input type="submit" value="Delete Selected"
class="button-primary"/>
</form>
- Find the ch8bt_admin_init function and add the following function call at the end of its body:
add_action( 'admin_post_delete_ch8bt_bug', 'delete_ch8bt_bug' );
- Navigate to the bottom of the file and add the following code block to provide an implementation for the delete_ch8bt_bug function responsible for processing deletion requests generated by the new form:
function delete_ch8bt_bug() {
// Check that user has proper security level
if ( !current_user_can( 'manage_options' ) ) {
wp_die( 'Not allowed' );
}
// Check if nonce field is present
check_admin_referer( 'ch8bt_deletion' );
// If bugs are present, cycle through array and call SQL
// command to delete entries one by one
if ( !empty( $_POST['bugs'] ) ) {
// Retrieve array of bugs IDs to be deleted
$bugs_to_delete = $_POST['bugs'];
global $wpdb;
foreach ( $bugs_to_delete as $bug_to_delete ) {
$query = 'DELETE from ' . $wpdb->get_blog_prefix();
$query .= 'ch8_bug_data WHERE bug_id = %d';
$wpdb->query( $wpdb->prepare( $query,
intval( $bug_to_delete ) ) );
}
}
// Redirect the page to the user submission form
wp_redirect( add_query_arg( 'page', 'ch8bt-bug-tracker',
admin_url( 'options-general.php' ) ) );
exit;
}
- Save and close the plugin file.
- Navigate to the new Bug Tracker item under the administration page's Settings menu to see the new interface elements that were added to the bug listing.