Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I only want to do bulk action on delete function. I have research so many example on google but still cant make it. Will be so much appreciate if someone be able to help me out on this. Below are my codes. When Select and choose delete on drop down and click apply, nothing is happen.

public function get_bulk_actions(){
    $action = array(
        "delete" =>"Delete"
    );
    return $action; 
}

public function get_columns(){
    $columns = array(
        "cb"=>"<input type='checkbox'/>",
        "No"=>"No",
        "Name"=>"Name",
        "Email"=>"Email",
        "Unique_Code"=>"Unique Code",
        "Created_date"=>"Created Date"
    );
        return $columns;
 }

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

public function process_bulk_action() {

// If the delete bulk action is triggered
 $action = $this->current_action();
        if( 'delete'===$action ) {
        $delete_ids = esc_sql( $_POST['post'] );
        // loop over the array of record IDs and delete them
            foreach ( $delete_ids as $did ) {
                global $wpdb;
                $wpdb->query($wpdb->prepare( "DELETE FROM mncplugin WHERE id='".$did."'"));
            }

                    wp_redirect( esc_url( add_query_arg() ) );
                    exit;
                    }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
216 views
Welcome To Ask or Share your Answers For Others

1 Answer

You have to call the method process_bulk_action() inside the method prepare_items()

Please find the gist which I created: https://gist.github.com/gvgvgvijayan/d46a52ca47309d1685ac91db3e9cf759#file-admin-table-tut-php-L272

Snippet:

public function prepare_items() {
    $columns               = $this->get_columns();
    $sortable              = $this->get_sortable_columns();
    $hidden                = array();
    $primary               = 'title';
    $this->_column_headers = array( $columns, $hidden, $sortable, $primary );
    $data                  = array();

    $this->process_bulk_action();

    $get_posts_obj = $this->get_posts_object();

    if ( $get_posts_obj->have_posts() ) {

        while ( $get_posts_obj->have_posts() ) {

            $get_posts_obj->the_post();

            $data[ get_the_ID() ] = array(
                'id'     => get_the_ID(),
                'title'  => get_the_title(),
                'type'   => ucwords( get_post_type_object( get_post_type() )->labels->singular_name ),
                'date'   => get_post_datetime(),
                'author' => get_the_author(),
            );
        }
        wp_reset_postdata();
    }

    $this->items = $data;

    $this->set_pagination_args(
        array(
            'total_items' => $get_posts_obj->found_posts,
            'per_page'    => $get_posts_obj->post_count,
            'total_pages' => $get_posts_obj->max_num_pages,
        )
    );
}

Update 1:

Place all of your class method calls inside the form tag as below snippet.

function show_list_table() {
    $mnc_list_table = new MNCTableClass();
    echo '<h3>MNC List</h3>';
    echo "<form method='post' name='frm_search_post' action='" . $_SERVER['PHP_SELF'] . "?page=alluserentries'>";
    $mnc_list_table->prepare_items();
    $mnc_list_table->display();
    $mnc_list_table->search_box("Search", "search_post_id");
    echo "</form>";
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...