Skip to content

WordPress – restrict user role from completely deleting posts

If you decide you have a type of user you’d like to add and edit posts but don’t wish for them to completely delete posts, here is a solution.

Note: the user can still move content to trash. From there, they won’t be able to delete.

function restrict_post_deletion($post_id){
 
    /*
     * Get logged in user details
     */
    $user_id    = get_current_user_id();
    $data       = get_userdata($user_id);
 
    /*
     * In this example, restrict 'editor' role
     * from completely deleting posts. 
     */
    if(in_array('editor', $data->roles)) {
        echo "You are not authorised to delete this content.";
        exit;
    }
}
 
add_action('delete_post', 'restrict_post_deletion', 10, 1);

About 

10 years + experience in web development working with lots of different technology.

Published inPHPWordPress

Be First to Comment

Leave a Reply

Your email address will not be published. Required fields are marked *