Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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?

share|improve this question
add comment

2 Answers

Don't let the action die, just do a redirect (to wherever you'd like):

function wpse_92155_before_delete_post() {
    wp_redirect(admin_url('edit.php'));
    exit();
} // function wpse_92155_before_delete_post
add_action('before_delete_post', 'wpse_92155_before_delete_post', 1);
share|improve this answer
add comment

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.

before

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;
            }
            </style>";
    }
}

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:

after

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.

share|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
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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