Disable delete user

When you click on “delete”, the action ‘delete_user’ will be launched: https://core.trac.wordpress.org/browser/tags/4.4.1/src/wp-admin/includes/user.php#L313

After that you can check, if the user has written at least one ‘portfolio’ post.

add_action('delete_user', 'sw_portfolio_check');

function sw_portfolio_check( $user_id ) {
    $result = new WP_Query(
                array(
                    'author'=>$user_id,
                    'post_type'=>'portfolio',
                    'posts_per_page'=>1,
                )
            );
    if ( count($result->posts) !== 0 ){
        wp_die("User has a portfolio and can't be deleted");
    }
}

Leave a Comment