简体   繁体   English

用于用户注册(注册)的 Wordpress REST API

[英]Wordpress REST API for user sign up (registration)

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?是否可以创建一个可以通过 WP REST API 工作的注册表单,以便访问者能够在我的网站上创建帐户?

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.当我以管理员身份登录时,这适用于 wp_rest nonce。

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?如何让访问者能够注册 REST API?

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.如果没有,只需将以下代码粘贴到您主题的 function.php 中,它应该像魅力一样工作。以下代码应该通过 REST API 将用户注册添加到您的 WordPress 网站。 It supports Registration of 'subscriber' and 'customer'.它支持“订阅者”和“客户”的注册。 Source code also available on Github .源代码也可在Github 上获得

Add it to your function.php将它添加到您的 function.php

add_action('rest_api_init', 'wp_rest_user_endpoints');
/**
 * Register a new user
 *
 * @param  WP_REST_Request $request Full details about 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']);
  // $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)) {
      // 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.)
      $response['code'] = 200;
      $response['message'] = __("User '" . $username . "' Registration was Successful", "wp-rest-user");
    } 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 seperate plugin.恕我直言,更好的方法是将附加功能作为单独的插件包含在内。 So even when your user changed theme, your api calls won't be affected.因此,即使您的用户更改了主题,您的 api 调用也不会受到影响。

Therefore I've developed a plugin for User Registration via REST API in WordPress.因此,我在 WordPress 中通过 REST API 开发了一个用于用户注册的插件 Better yet, it supports creating 'customer' for WooCommerce too!更好的是,它还支持为 WooCommerce 创建“客户”!

WP REST User , check it out if you want. WP REST 用户,如果需要,请查看。

You can use this two plugins for this:您可以为此使用这两个插件:

WordPress JSON API plugin WordPress JSON API 插件

and

JSON API User JSON API 用户

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.这两个插件都将有助于完成工作。您将需要 nonce ID 来调用由插件提供的注册 API。

this will explain , how it works!! 将解释,它是如何工作的!!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM