Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Where is the delete function? #14

blbwd opened this issue Jun 12, 2019 · 1 comment


None yet
2 participants
Copy link

commented Jun 12, 2019

No description provided.


This comment has been minimized.

Copy link

commented Jun 20, 2019

I used this generator and implemented delete myself. Here's what I added:

In yourname-functions.php I added a completely new method to handle deletes. It expects an array of 1 or more IDs.

function mj_delete_items ( $ids ) {
    global $wpdb;

    $ids = implode( ',', array_map( 'absint', $ids ) );
    $delQuery = "DELETE FROM " . $wpdb->prefix . "posts WHERE id IN ($ids)";

    return $wpdb->query( $delQuery );

In yourname-list-table.php I use name="id[]" for the list of IDs to bulk delete.

function column_cb($item) {
        return sprintf(
            '<input type="checkbox" name="id[]" value="%s" />',

Still in yourname-list-table.php I added "process_bulk_actions()" to handle the delete case. It gets the ID (or IDs) from the URL bar and builds them into array that it passes to mj_delete_items().

     * Set the bulk actions
     * @return array
    function get_bulk_actions() {
        $actions = array(
            'delete'  => __( 'Delete', 'mj' ),
        return $actions;

    function process_bulk_action() {

        // security check!
        if ( isset( $_POST['_wpnonce'] ) && ! empty( $_POST['_wpnonce'] ) ) {

            $nonce  = filter_input( INPUT_POST, '_wpnonce', FILTER_SANITIZE_STRING );
            $action = 'bulk-' . $this->_args['plural'];

            if ( ! wp_verify_nonce( $nonce, $action ) )
                wp_die( 'Nope! Security check failed!' );


        $action = $this->current_action();

        switch ( $action ) {
            case 'delete':
                if ('delete' === $this->current_action()) {
                    $ids = isset($_REQUEST['id']) ? $_REQUEST['id'] : array();
                    if (!is_array($ids)) $ids = array($_REQUEST['id']);

                    mj_delete_items($ids); //expects an array


Finally, in yourname-list-table.php, I added a call to process bulk action before getting the list of items. (I couldn't get this working for a while and my mistake was I needed to process bulk actions before getting all of the items.)

function prepare_items() { 

        // .. generated stuff here 

        // process bulk actions if any DO THIS FIRST 

        // now get all the items (minus any deleted ones from prior step) DO THIS SECOND 
        $this->items  = mj_get_all_items( $args ); //the generator should include this method call 

        $this->set_pagination_args( array(
            'total_items' => mj_get_item_count(),
            'per_page'    => $per_page
        ) );

Also, in views/yourname-list.php - I added a banner that tells the user how many items were deleted after the page reloads. This part isn't necessary but it's a nicer user experience. Here's the "form" part of the file (I kept the rest as it was generated, too).

<form method="get">
        <input type="hidden" name="page" value="<?php echo $_REQUEST['page'] ?>"/>  <!-- value="ttest_list_table">-->

        $list_table = new MJ_List_Table();

        $message = '';
        if ('delete' === $list_table->current_action()) {
            $message = '<div class="updated below-h2" id="message"><p>' . sprintf(__('Items deleted: %d', 'mj'), count($_REQUEST['id'])) . '</p></div>';
        echo $message;

        $list_table->search_box( 'search', 'search_id' );

I hope this helps someone else. I'm new to plugin development and found this generator really helpful in getting started. Also, this heavily-commented example helped me a lot:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.