Ask Before Moving Posts to the Trash
Shows a confirmation dialog when someone clicks "Trash" under a post, page or media item, or applies the Move to Trash bulk action, to prevent accidental removals.
Opens WPIDE on
Code
$wpide_trash_confirm = function () {
?>
<script>
document.addEventListener( 'click', function ( event ) {
var link = event.target.closest( '.row-actions .trash a, .row-actions .delete a, a.submitdelete' );
if ( link && ! window.confirm( 'Move this item to the trash?' ) ) {
event.preventDefault();
}
}, true );
document.addEventListener( 'submit', function ( event ) {
var form = event.target;
if ( ! form.id || form.id !== 'posts-filter' ) { return; }
var top = form.querySelector( '#bulk-action-selector-top' );
var bottom = form.querySelector( '#bulk-action-selector-bottom' );
var action = ( top && top.value !== '-1' ) ? top.value : ( bottom ? bottom.value : '' );
if ( action === 'trash' ) {
var count = form.querySelectorAll( 'tbody .check-column input:checked' ).length;
if ( count && ! window.confirm( 'Move ' + count + ' item(s) to the trash?' ) ) {
event.preventDefault();
}
}
}, true );
</script>
<?php
};
add_action( 'admin_footer-edit.php', $wpide_trash_confirm );
add_action( 'admin_footer-upload.php', $wpide_trash_confirm );
add_action( 'admin_footer-post.php', $wpide_trash_confirm );
Notes
Items in the trash can still be restored for 30 days; this only adds a safety prompt.
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.