Halloween Sale 15% off Ends in See the plans
PHP

Bulk Remove Featured Images

Adds a "Remove featured image" bulk action to post and page lists that clears the featured image from all selected items at once, without deleting the images from the Media Library.

Don’t have WPIDE yet? Install it on your site

Code

foreach ( array( 'post', 'page' ) as $wpide_type ) {
    add_filter( "bulk_actions-edit-{$wpide_type}", function ( $actions ) {
        $actions['wpide_remove_thumbnail'] = 'Remove featured image';
        return $actions;
    } );

    add_filter( "handle_bulk_actions-edit-{$wpide_type}", function ( $redirect, $action, $post_ids ) {
        if ( 'wpide_remove_thumbnail' !== $action ) {
            return $redirect;
        }
        $done = 0;
        foreach ( $post_ids as $post_id ) {
            if ( current_user_can( 'edit_post', $post_id ) && delete_post_thumbnail( $post_id ) ) {
                $done++;
            }
        }
        return add_query_arg( 'wpide_thumbs_removed', $done, $redirect );
    }, 10, 3 );
}

add_action( 'admin_notices', function () {
    if ( isset( $_GET['wpide_thumbs_removed'] ) ) {
        printf( '<div class="notice notice-success is-dismissible"><p>Removed the featured image from %d items.</p></div>', absint( $_GET['wpide_thumbs_removed'] ) );
    }
} );

Notes

The images stay in the Media Library and can be set again at any time.

How to add this snippet

  1. Install WPIDE on your WordPress site (it is free) and open Code Snippets.
  2. Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
  3. It is added inactive: review the code and settings, then switch it on.

Have a snippet worth sharing?

Publish it to the library from WPIDE or right here. Sign in with any WordPress site that runs WPIDE, free or Pro. Public snippets are reviewed by the WPIDE team before they appear.

Which site should get this snippet?

Enter the address of your WordPress site. WPIDE opens there in a new tab with the snippet ready to review and save.

Remembered in this browser only, so you are asked once.

Report this snippet

Tell the WPIDE team what is wrong: broken code, something harmful, spam or copied work.