javascript - Wordpress REST API for user sign up (registration) - Stack Overflow

Is it possible to create a sign up form that can work through WP REST API for visitors to be able to cr

Is it possible to create a sign up form that can work through WP REST API for visitors to be able to create accounts on my site?

I can create such a form and use it to create new users. This works with wp_rest nonce when I am logged in as administrator.

But if there is a visitor which is not logged in, the same form does not work of course. I think this is a question of authentication. Can you suggest a general idea how this can work? How can I allow visitors to be able to sign up with REST API?

Is it possible to create a sign up form that can work through WP REST API for visitors to be able to create accounts on my site?

I can create such a form and use it to create new users. This works with wp_rest nonce when I am logged in as administrator.

But if there is a visitor which is not logged in, the same form does not work of course. I think this is a question of authentication. Can you suggest a general idea how this can work? How can I allow visitors to be able to sign up with REST API?

Share Improve this question asked Apr 25, 2017 at 17:51 dziunglesdziungles 311 gold badge1 silver badge4 bronze badges 1
  • while WP doesn't provide this out of the box, there are plugins that can do this, and the 3rd party authentication plugins might provide solutions such as the OAuth 2 support. There's also a session management plugin at github./humanmade/rest-sessions that may be helpful by the same people who helped build the REST API – Tom J Nowell Commented Mar 8, 2023 at 16:41
Add a ment  | 

2 Answers 2

Reset to default 3

Hopefully you've found your answers already!

If not, just paste the following code in your theme's function.php, and it should work like a charm.The following code should add User Registration via REST API to your WordPress Website. It supports Registration of 'subscriber' and 'customer'. Source code also available on Github.

Add it to your function.php

add_action( 'rest_api_init', 'wp_rest_user_endpoints' );

function wp_rest_user_endpoints() {
  /**
   * Handle Register User request.
   */
  register_rest_route( 'wp/v2', 'users/register', 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 = null ) {
  $response = array();
  $parameters = $request->get_json_params();
  $username = sanitize_user( $parameters['username'] );
  $email = sanitize_email( $parameters['email'] );
  $password = sanitize_text_field( $parameters['password'] );
  // $role = sanitize_text_field( $parameters['role']);

  $error = new WP_Error();
  if ( empty( $username ) ) {
    $error->add( 400, __( "Username 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;
  }
  if ( empty( $password ) ) {
    $error->add( 404, __( "Password field 'password' is required.", 'wp-rest-user' ), array( 'status' => 400 ) );
    return $error;
  }
  // if (empty($role)) {
  //  $role = 'subscriber';
  // } else {
  //     if ( $GLOBALS['wp_roles']->is_role( $role ) ) {
  //      // Silence is gold
  //     } else {
  //    $error->add( 405, __("Role field 'role' is not a valid. Check your User Roles from Dashboard.", 'wp_rest_user' ), array('status' => 400 ) );
  //    return $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 ) ) {
      // Get 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' );
      }
      // Get User Data (Non-Sensitive, Pass to front end.)
      $response['code'] = 200;
      $response['message'] = sprintf( __( "User '%s' Registration was Successful", 'wp-rest-user' ), $username );
    } 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 );
}

IMHO, a more better way would to include the additional function as a separate plugin. So even when your user changed theme, your api calls won't be affected.

Therefore I've developed a plugin for User Registration via REST API in WordPress. Better yet, it supports creating 'customer' for WooCommerce too!

WP REST User, check it out if you want.

You can use this two plugins for this:

WordPress JSON API plugin

and

JSON API User

These both plugins will help to get the job done.You will need nonce ID to call registration API,which will be given by plugin.

this will explain , how it works!!

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742297623a4417440.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信