使用从WP帖子列表表继承的表的Wordpress批量垃圾操作

时间:2011-11-11 13:26:35

标签: wordpress wordpress-plugin

在一个显示自定义类型帖子表的插件中,我想添加批量操作的选项。除了复选框,我还显示了批量操作的控件,但是当我点击Apply时,没有任何反应。

我想这应该是一个动作,但我找不到哪一个。我试过了trash_post,但它没有被击中。

我如何执行批量删除,因为我不知道我是否在正确的轨道上。

1 个答案:

答案 0 :(得分:2)

您必须在类中定义扩展WP_List_Table的批量操作数组,然后在显示功能中,您需要定义各个操作。

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

您可以使用wp_posts_list_table作为完成方式的示例:

$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 &#8220;%s&#8221;' ), $title ) ) . '" rel="permalink">' . __( 'Preview' ) . '</a>';
                    } elseif ( 'trash' != $post->post_status ) {
                        $actions['view'] = '<a href="' . get_permalink( $post->ID ) . '" title="' . esc_attr( sprintf( __( 'View &#8220;%s&#8221;' ), $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;

您还必须添加javascript以生成ajax请求并从隐藏的inline_ div中填入数据。查看或重复使用inline-edit-post-dev.js中的代码。