Login and register by API

1 week ago 13

When we instal WordPress with Softaculous, a Login fastener appears and we log successful to the tract without entering the username and password we pressed.

We person a tract wherever we merchantability integer subscriptions. We negociate subscriptions for assorted websites here. Now we request to integrate a tract made with WordPress here.

When our lawsuit receives a tract subscription, we program to make a idiosyncratic for the WordPress tract with the codification below.

add_action('rest_api_init', 'wp_rest_user_endpoints'); /** * Register a caller user * * @param WP_REST_Request $request Full details astir the request. * @return array $args. **/ function wp_rest_user_endpoints($request) { /** * Handle Register User request. */ register_rest_route('wp/v2', 'users/register', array( 'methods' => 'POST', 'callback' => 'wc_rest_user_endpoint_handler', )); } function wc_rest_user_endpoint_handler($request = null) { $response = array(); $parameters = $request->get_json_params(); $username = sanitize_text_field($parameters['username']); $email = sanitize_text_field($parameters['email']); $password = sanitize_text_field($parameters['password']); $error = caller WP_Error(); if (empty($username)) { $error->add(400, __("Username tract 'username' is required.", 'wp-rest-user'), array('status' => 400)); instrumentality $error; } if (empty($email)) { $error->add(401, __("Email tract 'email' is required.", 'wp-rest-user'), array('status' => 400)); instrumentality $error; } if (empty($password)) { $error->add(404, __("Password tract 'password' is required.", 'wp-rest-user'), array('status' => 400)); instrumentality $error; } $user_id = username_exists($username); if (!$user_id && email_exists($email) == false) { $user_id = wp_create_user($username, $password, $email); if (!is_wp_error($user_id)) { $user = get_user_by('id', $user_id); $user->set_role('subscriber'); $response['code'] = 200; $response['message'] = __("User '" . $username . "' Registration was Successful", "wp-rest-user"); } other { instrumentality $user_id; } } other { $error->add(406, __("Email already exists, delight effort 'Reset Password'", 'wp-rest-user'), array('status' => 400)); instrumentality $error; } instrumentality caller WP_REST_Response($response, 123); }

Then, we privation to enactment a Login fastener connected our site, arsenic I mentioned supra successful the Softaculous structure, and nonstop it to the WordPress website arsenic a subordinate login.

Q1. How tin we bash this login event? Can we login via API oregon bash we request thing different? I'm asking due to the fact that I've seen ways to log successful with a cookie.

We besides enactment a subordinate login signifier connected the WordPress website. This signifier triggers an API that checks whether the rank we person written connected our tract is progressive oregon not.

Q2. When it gets Ok effect from API, it volition instrumentality to WordPress tract and log in. How tin we marque this turn?

Read Entire Article

Our Ventures