使用 Wordpress JSON API 注册/登录用户

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13679001/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 18:38:53  来源:igfitidea点击:

Register/login user with Wordpress JSON API

jsonwordpresswordpress-plugin

提问by narek.gevorgyan

I want to create mobile app for one wordpress website. I have integrated the wordpress json plugin. I'm not sure where I can find service for user registration and login. Please advice.

我想为一个 wordpress 网站创建移动应用程序。我已经集成了wordpress json 插件。我不确定在哪里可以找到用户注册和登录服务。请指教。

采纳答案by Salam El-Banna

To register a user thiswill show you exactly how to register one on the database by simply calling a url and adding data to it using GET Method. Now to do so from a mobile app you just have to make an http request to a url containing all the data required for the user. Thiswill show you how to make a request from Android.

要注册用户,将向您展示如何通过简单地调用 url 并使用GET Method向其添加数据来准确地在数据库上注册一个用户。现在要从移动应用程序执行此操作,您只需向包含用户所需的所有数据的 url 发出 http 请求。将向您展示如何从 Android 发出请求。

This is just for registering users there will be another plugin JSON APi Authused in order to login a user.

这仅用于注册用户,将使用另一个插件JSON APi Auth来登录用户。

These are the basics since I don't have much time now, when I do, I will provide full details and example. But for now this shall do it

这些是基础知识,因为我现在没有太多时间,当我有时间时,我将提供完整的细节和示例。但现在这将做到

回答by Ganesh Hargude

1.Paste Following code in your themes function.phpfile.

1.在您的主题function.php文件中粘贴以下代码。

2.Make sure that WP-REST-APIplugin Should be installed on wordpress site

2.确保WP-REST-API插件应该安装在wordpress站点上

add_action( 'rest_api_init', 'register_api_hooks' );

function register_api_hooks() {
  register_rest_route(
    'custom-plugin', '/login/',
    array(
      'methods'  => 'POST',
      'callback' => 'login',
    )
  );
}

function login($request){
    $creds = array();
    $creds['user_login'] = $request["username"];
    $creds['user_password'] =  $request["password"];
    $creds['remember'] = true;
    $user = wp_signon( $creds, false );

    if ( is_wp_error($user) )
      echo $user->get_error_message();

    return $user;
}

add_action( 'after_setup_theme', 'custom_login' );

Then your API will be created as

然后您的 API 将被创建为

http://www.url.com/wp-json/custom-plugin/login

Try it with Postman You will get 200as a response and user info

与 Postman 一起尝试,您将收到200回复并user info

回答by The Dead Guy

I was able to figure out both login and signupusing @Salam El-Bannas' link, in case any one still needs this here you go:

我能够使用@Salam El-Bannas 的链接弄清楚登录和注册,以防万一有人仍然需要这个,你去吧:

All through you need two plugins to get the job done:

您需要两个插件才能完成工作:

WordPress JSON API plugin

WordPress JSON API 插件

and

JSON API User

JSON API 用户

  1. For registration of users:You need nonce IDwhich will be a part of the registration params in the GET url process (this is provided by the plugins). Read @Salam El-Bannas' linkfor more understanding. The methodology i used in android was that immediately the page loads I block the UI with "Connecting..." message while in the background am getting the nonce ID once its done the UI Blocking disappears and the nonce ID is added to a special non-editable EditText field created for the nonce ID, the user then enters his/her required credentials and I authenticate that its valid before connecting to the server using the nonce ID i got. the result from the server is JSON so i can handle the rest in android code using volley and GSON.

  2. For Login of users: You only need cookies ID generated by the plugin eg in my case http://localhost/mylocalhost/my_api_base/user/generate_auth_cookie/?insecure=cool&[email protected]&password=xxxv678

  1. 对于用户注册:您需要nonce ID,它将成为 GET url 过程中注册参数的一部分(这由插件提供)。阅读@Salam El-Bannas 的链接以获得更多理解。我在 android 中使用的方法是,页面加载后立即使用“正在连接...”消息阻止 UI,而在后台获取 nonce ID 一旦完成 UI Blocking 消失,nonce ID 被添加到一个特殊的非- 为 nonce ID 创建的可编辑 EditText 字段,然后用户输入他/她所需的凭据,并在使用我获得的 nonce ID 连接到服务器之前验证其有效。服务器的结果是 JSON,因此我可以使用 volley 和 GSON 在 android 代码中处理其余部分。

  2. 对于用户登录:您只需要插件生成的 cookie ID,例如在我的情况下 http://localhost/mylocalhost/my_api_base/user/generate_auth_cookie/?insecure=cool&[email protected]&password=xxxv678

and the result was this json;

结果是这个json;

{"status":"ok","cookie":"xxxx|1486130938|Ot6yAX7iU773JnQ2zfE8sdmjt09LhHqDKSYBqtekuha|7fe58a35ab9f260c9bced9148f5cf9ae3ab56c16d7d9ce3b2db7da651d4d937d","cookie_name":"wordpress_logged_in_4132d8131ebbc6760d21627637bd4b20","user":{"id":1,"username":"administrator","nicename":"administrator","email":"[email protected]","url":"","registered":"2016-11-02 17:46:19","displayname":"xxxx","firstname":"","lastname":"","nickname":"xxxxwedds","description":"","capabilities":"","avatar":null}}

{"status":"ok","cookie":"xxxx|1486130938|Ot6yAX7iU773JnQ2zfE8sdmjt09LhHqDKSYBqtekuha|7fe58a35ab9f260c9bced9148f5cf9ae3ab56c16d7d9ce3b2db7da651d4d937d","cookie_name":"wordpress_logged_in_4132d8131ebbc6760d21627637bd4b20","user":{"id":1,"username":"administrator","nicename":"administrator","email":"[email protected]","url":"","registered":"2016-11-02 17:46:19","displayname":"xxxx","firstname":"","lastname":"","nickname":"xxxxwedds","description":"","capabilities":"","avatar":null}}

then you have to read this nice tutorialto use the resulting JSON in android (that's if you are new to android login) else just use your discretion.

那么你必须阅读这个很好的教程才能在 android 中使用生成的 JSON(如果你是 android 登录的新手),否则只需使用你的判断力。

Its really really really simple and interesting once you follow my lined up process.

一旦你按照我的排队过程,它真的非常非常简单和有趣。

回答by WebMat

Use with clean Class

与干净的类一起使用

// Register REST API endpoints
class Login_REST_API_Endpoints {

    /**
     * Register the routes for the objects of the controller.
     */
    public static function register_endpoints() {
        // endpoints will be registered here
        register_rest_route( 'wp', '/login', array(
            'methods' => 'GET',
            'callback' => array( 'Login_REST_API_Endpoints', 'login' ),
        ) );
    }

    /**
     * @param WP_REST_Request $request Full data about the request.
     * @return WP_Error|WP_REST_Request
     */
    public static function login( $request ) {


        $data = array();
        $data['user_login'] = $request["username"];
        $data['user_password'] =  $request["password"];
        $data['remember'] = true;
        $user = wp_signon( $data, false );

        if ( !is_wp_error($user) )
          return $user;
    }

}
add_action( 'rest_api_init', array( 'Login_REST_API_Endpoints', 'register_endpoints' ) );