Bulk Action to Move Posts Back to Draft
Adds "Move to draft" to the Bulk actions menu for posts and pages, to unpublish several items at once.
Opens WPIDE on
Code
// Post types that get the bulk action.
$wpide_to_draft_types = array( 'post', 'page' );
foreach ( $wpide_to_draft_types as $wpide_draft_type ) {
add_filter( "bulk_actions-edit-{$wpide_draft_type}", function ( $actions ) {
$actions['wpide_to_draft'] = 'Move to draft';
return $actions;
} );
// WordPress checks the bulk action nonce before this filter runs.
add_filter( "handle_bulk_actions-edit-{$wpide_draft_type}", function ( $redirect, $action, $post_ids ) {
if ( 'wpide_to_draft' !== $action ) {
return $redirect;
}
$done = 0;
foreach ( (array) $post_ids as $post_id ) {
if ( current_user_can( 'edit_post', $post_id ) && 'draft' !== get_post_status( $post_id ) ) {
$result = wp_update_post( array( 'ID' => (int) $post_id, 'post_status' => 'draft' ), true );
$done += is_wp_error( $result ) ? 0 : 1;
}
}
return add_query_arg( 'wpide_drafted', $done, $redirect );
}, 10, 3 );
}
add_action( 'admin_notices', function () {
if ( isset( $_GET['wpide_drafted'] ) ) {
$count = absint( $_GET['wpide_drafted'] );
printf( '<div class="notice notice-success is-dismissible"><p>%s</p></div>', esc_html( sprintf( _n( '%d item moved to draft.', '%d items moved to draft.', $count ), $count ) ) );
}
} );
How to add this snippet
- Install WPIDE on your WordPress site (it is free) and open Code Snippets.
- Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
- It is added inactive: review the code and settings, then switch it on.