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) {

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):

function wpse_92155_before_delete_post() {
} // function wpse_92155_before_delete_post
add_action('before_delete_post', 'wpse_92155_before_delete_post', 1);
| improve this answer | |
  • Qui old, I know, but how this work with AJAX calls and XML-RPC requests? I don't think that in these cases, redirection is the right solution. – Andrea Sciamanna Jul 18 '14 at 8:33

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:

add_action( 'admin_head-edit.php', 'hide_delete_css_wpse_92155' );
add_filter( 'post_row_actions', 'hide_row_action_wpse_92155', 10, 2 );
add_filter( 'page_row_actions', 'hide_row_action_wpse_92155', 10, 2 );

function hide_delete_css_wpse_92155()
    if( isset( $_REQUEST['post_status'] ) && 'trash' == $_REQUEST['post_status'] ) 
        echo "<style>
            .alignleft.actions:first-child, #delete_all {
                display: none;

function hide_row_action_wpse_92155( $actions, $post ) 
    if( isset( $_REQUEST['post_status'] ) && 'trash' == $_REQUEST['post_status'] ) 
        unset( $actions['delete'] );
    return $actions; 

This is the result:


Note of interest

There is no hook cpt_row_actions. The hooks page_row_actions and post_row_actions are applied if the post type is hierarchical or not, respectively.

| improve this answer | |
  • Mkay. +1 to both you and @tf. I couldn't upvote his one-liner before, because further information pending I could have only assessed its quality by further research on the subject. Yet another answer thoroughly put into context. – Johannes Pille Mar 25 '13 at 0:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.