1

I've added Archived bulk Action to my CPT, but my problem is that everything work fine only if we select the action in the second dropdown.

That's how i've added the new Bulk Action

add_action('admin_head', 'add_bulk_action');
function add_bulk_action() {
    global $wp_list_table;
    $current_screen = get_current_screen();

    if($current_screen->id == 'edit-programmation') {
            $wp_list_table->_actions = array_merge(
            $wp_list_table->get_bulk_actions(),
            array( 'archive' => __( 'Archiver' ) )
        );
    }
}

And my custom bulk action verification

add_action('load-edit.php', 'custom_bulk_action');
function custom_bulk_action() {
global $typenow;
$post_type = $typenow;

if($post_type == 'programmation') {

    // get the action
    $wp_list_table = _get_list_table('WP_Posts_List_Table');  // depending on your resource type this could be WP_Users_List_Table, WP_Comments_List_Table, etc
    $action = $wp_list_table->current_action();

    /* $action return -1 if I don't choose my action from the bottom dropdown... */

    $allowed_actions = array("archive");
    if(!in_array($action, $allowed_actions)) return;


    // security check
    check_admin_referer('bulk-posts');

    // make sure ids are submitted.  depending on the resource type, this may be 'media' or 'ids'
    if(isset($_REQUEST['post'])) {
        $post_ids = array_map('intval', $_REQUEST['post']);
    }

    if(empty($post_ids)) return;

    // this is based on wp-admin/edit.php
    $sendback = remove_query_arg( array('archived', 'untrashed', 'deleted', 'ids'), wp_get_referer() );
    if ( ! $sendback )
        $sendback = admin_url( "edit.php?post_type=$post_type" );

    $pagenum = $wp_list_table->get_pagenum();
    $sendback = add_query_arg( 'paged', $pagenum, $sendback );

    switch($action) {
        case 'archive':

            $archived = 0;
            foreach( $post_ids as $post_id ) {

                if ( !perform_archiving($post_id) )
                    wp_die( __('Error while archiving spectacle.') );

                $archived++;
            }
            $sendback = add_query_arg( array('archived' => $archived, 'ids' => join(',', $post_ids) ), $sendback );
            break;

        default: return;
    }

    $sendback = remove_query_arg( array('action', 'action2', 'tags_input', 'post_author', 'comment_status', 'ping_status', '_status',  'post', 'bulk_edit', 'post_view'), $sendback );

    wp_redirect($sendback);
    exit();
}
}

Thank's for your help.

1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.