No route was found matching the URL and request method. I don’t understand where the problem is

Question

When I send parameters, I get this: No route was found matching the URL and request method.

/** 
 * Add json data on plugin.
 * 
 * */
add_action('rest_api_init', 'register_api_hooks');
function register_api_hooks() {
  register_rest_route(
    'passwordless_register/v0', '/register/(?P<name>[a-zA-Z0-9-]+)/(?P<email>[a-zA-Z0-9-]+)/?aam-jwt=(?P<token>[a-zA-Z0-9-]+)',
    array(
      'methods'  => 'POST',
      'callback' => 'wc_rest_user_endpoint_handler',
    )
  );
}

/**
 * Register a new user
 *
 * @param  WP_REST_Request $request Full details about the request.
 * @return array $args.
 **/
function wc_rest_user_endpoint_handler($request) {
  $request = new WP_REST_Request( 'POST', 'passwordless_register/v0/register/(?P<name>[a-zA-Z0-9-]+)/(?P<email>[a-zA-Z0-9-]+)/?aam-jwt=(?P<token>[a-zA-Z0-9-]+)' );
  $username = $request['name'];
  $email = $request['email'];
  $response = array();
  $error = new WP_Error();
  if (empty($username)) {
    $error->add(400, __("name field 'username' is required.", 'wp-rest-user'), array('status' => 400));
    return $error;
  }
  if (empty($email)) {
    $error->add(401, __("Email field 'email' is required.", 'wp-rest-user'), array('status' => 400));
    return $error;
  }
  $user_id = username_exists($username);
  if (!$user_id && email_exists($email) == false) {  
    $password = wp_generate_password( 20, false );
    $user_id = wp_create_user($username, $password, $email);
    if (!is_wp_error($user_id)) {
      // Ger User Meta Data (Sensitive, Password included. DO NOT pass to front end.)
      $user = get_user_by('id', $user_id);
      // $user->set_role($role);
      $user->set_role('subscriber');
      // WooCommerce specific code
      if (class_exists('WooCommerce')) {
        $user->set_role('customer');
      }
      // Ger User Data (Non-Sensitive, Pass to front end.)
      wp_nonce_field( 'wpa_passwordless_login_request', 'nonce', false );
      $unique_url = wpa_generate_url( $email , $nonce );
      $response['code'] = 200;
      $response['message'] = __("User '" . $username . "' Registration was Successful", "wp-rest-user");
      $response['mail'] = __("Mail '" . $email . "' Registration was Successful", "wp-rest-email");
      $response['password'] =  __("Pass '" . $password . "' Registration was Successful", "wp-rest-pass");
      $response['url'] =  __("Link '" . $unique_url . "' Registration was Successful", "wp-rest-url");
    } else {
      return $user_id;
    }
  } else {
    $error->add(406, __("Email already exists, please try 'Reset Password'", 'wp-rest-user'), array('status' => 400));
    return $error;
  }
  return new WP_REST_Response($response, 123);          
}
add_action( 'after_setup_theme', 'passwordless_register/v0' );
0
Ahmet Kahraman 4 months 0 Answers 23 views 0

Leave an answer