wp-generators icon indicating copy to clipboard operation
wp-generators copied to clipboard

Where is the delete function?

Open sanjaybhowmick opened this issue 6 years ago • 2 comments
trafficstars

sanjaybhowmick avatar Jun 12 '19 13:06 sanjaybhowmick

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" />',
            $item->ID
        );
    }

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
                }
                break;

            default:
                return;
                break;
        }
    }

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 
        $this->process_bulk_action();

        // 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">-->

        <?php
        $list_table = new MJ_List_Table();
        $list_table->prepare_items();

        $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' );
        $list_table->display();
        ?>
    </form>

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: https://github.com/pmbaldha/WP-Custom-List-Table-With-Database-Example/blob/master/custom-list-table-db-example.php

manderly avatar Jun 20 '19 23:06 manderly

I can confirm that MJGrant's work does it's job, would be great to have notification before actually anything is deleted and I would still like to add to trash option as well, would be great to have it.

Thanks.

KovaZg avatar Feb 09 '20 14:02 KovaZg