Add Custom Bulk Actions to the Posts List in WordPress

Add a custom option to the bulk actions dropdown on the posts list screen. This example marks selected posts as “Featured”. For more on WordPress hooks.

add_filter( 'bulk_actions-edit-post', function ( $actions ) {
    $actions['mark_featured'] = 'Mark as Featured';
    return $actions;
} );

add_filter( 'handle_bulk_actions-edit-post', function ( $redirect_to, $action, $post_ids ) {
    if ( $action !== 'mark_featured' ) {
        return $redirect_to;
    }

    foreach ( $post_ids as $post_id ) {
        update_post_meta( $post_id, '_is_featured', '1' );
    }

    return add_query_arg( 'marked_featured', count( $post_ids ), $redirect_to );
}, 10, 3 );

add_action( 'admin_notices', function () {
    if ( empty( $_REQUEST['marked_featured'] ) ) {
        return;
    }

    $count = intval( $_REQUEST['marked_featured'] );
    printf(
        '<div class="notice notice-success is-dismissible"><p>%d post(s) marked as featured.</p></div>',
        $count
    );
} );

Add to functions.php. Change mark_featured and the meta key to fit your use case.

Discussion
[  0 Comments  ]

Leave a Comment

To add code, use the buttons below. For instance, click the PHP button to insert PHP code within the shortcode. If you notice any typos, please let us know!