add_action('rest_api_init', function () {
    register_rest_route('custom/v1', '/user-exists', array(
        'methods' => 'POST',
        'callback' => 'check_user_exists',
        'permission_callback' => '__return_true',
        'args' => array()
    ));
});

function check_user_exists(WP_REST_Request $request) {
    $email = $request->get_param('email');
    $username = $request->get_param('username');

    if ($email) {
        $user = get_user_by('email', $email);
    } elseif ($username) {
        $user = get_user_by('login', $username);
    } else {
        return new WP_Error('invalid_request', 'Devi fornire un email o uno username.', array('status' => 400));
    }

    if ($user) {
        return array('exists' => true, 'user_id' => $user->ID);
    } else {
        return array('exists' => false);
    }
}