For auditing issue, I only want a post can be trashed, but not deleted – for all users.
So I have a plugin like
add_action('before_delete_post', function($id) {
wp_die(0);
});
But seems not all delete action are ajax, so it will be show a black screen with return an error page with result “0”
Feature wise the above code is ok, but are there any better way?
Don’t let the action die, just do a redirect (to wherever you’d like):
I would use the hook
before_delete_post
as the last layer of protection against deletion (using @tf’s solution, and which is the correct Answer).And first remove the “Delete” options from sight. The following hides the Bulk Actions and the Empty Trash button, and removes the Delete Permanently row action.
With this code:
This is the result:
Note of interest
There is no hook
cpt_row_actions
. The hookspage_row_actions
andpost_row_actions
are applied if the post type is hierarchical or not, respectively.This is an old post, but for others still seeking an answer, I think there’s a more elegant way â filtering
user_has_cap
. This allows you to remove the users rights to delete a post under whatever circumstances you determine. Filteringuser_has_cap
automatically removes the UI elements and the action in one simple function and works no matter how the attempt to delete is made. See this post for more details.Â