WordPress bulk trash action using table inherited from WP Posts List Table

In a plugin which shows a table of posts of a custom type, I wanted to add the option for bulk actions. Besides the checkboxes I displayed the control for bulk actions, but when I click Apply, nothing happens.

It’s supposed to be an action, I guess, but I couldn’t find which one. I tried trash_post, but it doesn’t get hit.

Read More

How do I perform a bulk delete anyway, since I’ve no idea if I’m even on the right track.

Related posts

Leave a Reply

1 comment

  1. You will have to define your bulk actions array in your class that extends WP_List_Table then in your display function you will need to define the individual actions.

    function get_bulk_actions() {
            $actions = array();
    
            if ( $this->is_trash )
                $actions['untrash'] = __( 'Restore' );
            else
                $actions['edit'] = __( 'Edit' );
    
            if ( $this->is_trash || !EMPTY_TRASH_DAYS )
                $actions['delete'] = __( 'Delete Permanently' );
            else
                $actions['trash'] = __( 'Move to Trash' );
    
            return $actions;
        }
    

    You can use wp_posts_list_table as an example of how it’s done:

    $actions = array();
                    if ( $can_edit_post && 'trash' != $post->post_status ) {
                        $actions['edit'] = '<a href="' . get_edit_post_link( $post->ID, true ) . '" title="' . esc_attr( __( 'Edit this item' ) ) . '">' . __( 'Edit' ) . '</a>';
                        $actions['inline hide-if-no-js'] = '<a href="#" class="editinline" title="' . esc_attr( __( 'Edit this item inline' ) ) . '">' . __( 'Quick&nbsp;Edit' ) . '</a>';
                    }
                    if ( current_user_can( $post_type_object->cap->delete_post, $post->ID ) ) {
                        if ( 'trash' == $post->post_status )
                            $actions['untrash'] = "<a title='" . esc_attr( __( 'Restore this item from the Trash' ) ) . "' href='" . wp_nonce_url( admin_url( sprintf( $post_type_object->_edit_link . '&amp;action=untrash', $post->ID ) ), 'untrash-' . $post->post_type . '_' . $post->ID ) . "'>" . __( 'Restore' ) . "</a>";
                        elseif ( EMPTY_TRASH_DAYS )
                            $actions['trash'] = "<a class='submitdelete' title='" . esc_attr( __( 'Move this item to the Trash' ) ) . "' href='" . get_delete_post_link( $post->ID ) . "'>" . __( 'Trash' ) . "</a>";
                        if ( 'trash' == $post->post_status || !EMPTY_TRASH_DAYS )
                            $actions['delete'] = "<a class='submitdelete' title='" . esc_attr( __( 'Delete this item permanently' ) ) . "' href='" . get_delete_post_link( $post->ID, '', true ) . "'>" . __( 'Delete Permanently' ) . "</a>";
                    }
                    if ( $post_type_object->public ) {
                        if ( in_array( $post->post_status, array( 'pending', 'draft', 'future' ) ) ) {
                            if ( $can_edit_post )
                                $actions['view'] = '<a href="' . esc_url( add_query_arg( 'preview', 'true', get_permalink( $post->ID ) ) ) . '" title="' . esc_attr( sprintf( __( 'Preview “%s”' ), $title ) ) . '" rel="permalink">' . __( 'Preview' ) . '</a>';
                        } elseif ( 'trash' != $post->post_status ) {
                            $actions['view'] = '<a href="' . get_permalink( $post->ID ) . '" title="' . esc_attr( sprintf( __( 'View “%s”' ), $title ) ) . '" rel="permalink">' . __( 'View' ) . '</a>';
                        }
                    }
    
                    $actions = apply_filters( is_post_type_hierarchical( $post->post_type ) ? 'page_row_actions' : 'post_row_actions', $actions, $post );
                    echo $this->row_actions( $actions );
    
                    get_inline_data( $post );
                    echo '</td>';
                break;
    

    You also have to add the javascript to make the ajax requests and fill in the data from the hidden inline_ div. Take a look or reuse the code from inline-edit-post-dev.js.