I need to disable the permanent deletion of posts (it's ok to trash them, but users shouldn't be able to delete them from trash).
I did that by hooking into before_delete_post and redirecting (thanks to this thread):
function disable_delete_post() {
wp_redirect(admin_url('edit.php'));
exit();
}
add_action('before_delete_post', 'disable_delete_post', 1);
This works, however, users report that sometimes (not clear when) they are redirected when they're trying to open the editor of a (published) post, which prevents the post from being edited at all.
So it seems the function is executed even though the user is not trying to delete (in fact, I'm also hiding the "delete permanently" link via CSS, so users shouldn't be able to trigger this in the first place).
Does anybody have any ideas why before_delete_posts might run outside of post deletion?
Hiding the link via CSS is not enough as a solution, it needs to be more secure (someone with CSS skills could circumvent it).
** Alternative Solution I'm considering: **
Adding a conditional to check for post status and only redirect when status of post is 'trash':
function disable_delete_post($old_status) {
if ($old_status = 'trash' ) {
wp_redirect(admin_url('edit.php'));
exit();
}
}
add_action('before_delete_post', 'disable_delete_post', 1);
I'm hoping this fixes the problem but because I don't understand the root I'm hesitant to use this on a live site.