admin管理员组文章数量:1415446
I installed the "JWT Authentication for WP REST API" plugin for user authentication through an Ionic app. Although authentication was successful, attempting to find a way to register users from the mobile app proved to be a particularly difficult task.
Is there a way to register users from the API given by WordPress? Or is there some preferred way to implement this on the admin console to enable this behavior?
I'm completely helpless here. The data is 'POST'ed through query parameters to the base url, and gives a 302 response, but when I resend the request through Fiddler it gives a 200 OK. And when I attempt to replicate the request on Postman it also gives a 200 OK.
I considered the JSON API plugin with the JSON API USER plugin route, but these don't seem to be under active development. I've read somewhere this gets done with GET and a cookie or something?
I installed the "JWT Authentication for WP REST API" plugin for user authentication through an Ionic app. Although authentication was successful, attempting to find a way to register users from the mobile app proved to be a particularly difficult task.
Is there a way to register users from the API given by WordPress? Or is there some preferred way to implement this on the admin console to enable this behavior?
I'm completely helpless here. The data is 'POST'ed through query parameters to the base url, and gives a 302 response, but when I resend the request through Fiddler it gives a 200 OK. And when I attempt to replicate the request on Postman it also gives a 200 OK.
I considered the JSON API plugin with the JSON API USER plugin route, but these don't seem to be under active development. I've read somewhere this gets done with GET and a cookie or something?
Share Improve this question asked Sep 9, 2017 at 19:29 cuzoxcuzox 1211 silver badge3 bronze badges 1- I saw your post on the WordPress support forum; did you get the JWT Authentication for WP REST API plugin working on WordPress? If so, please share how. Thanks. – HBCondo Commented Sep 19, 2018 at 2:32
2 Answers
Reset to default 1This does not use the API, but it's a script I've used a hundred times over the years, always works. Simply drop it in the root of your install and then access the file directly. Remember to delete the file immediately afterward. I do not remember where I originally obtained this script.
<?php
// ADD NEW ADMIN USER TO WORDPRESS
// ----------------------------------
// Put this file in your Wordpress root directory and run it from your browser.
// Delete it when you're done.
require_once('wp-blog-header.php');
require_once('wp-includes/registration.php');
// ----------------------------------------------------
// CONFIG VARIABLES
// Make sure that you set these before running the file.
$newusername = 'your_username';
$newpassword = 'youer_password';
$newemail = '[email protected]';
// ----------------------------------------------------
// This is just a security precaution, to make sure the above "Config Variables"
// have been changed from their default values.
if ( $newpassword != 'YOURPASSWORD' &&
$newemail != '[email protected]' &&
$newusername !='YOURUSERNAME' )
{
// Check that user doesn't already exist
if ( !username_exists($newusername) && !email_exists($newemail) )
{
// Create user and set role to administrator
$user_id = wp_create_user( $newusername, $newpassword, $newemail);
if ( is_int($user_id) )
{
$wp_user_object = new WP_User($user_id);
$wp_user_object->set_role('administrator');
echo 'Successfully created new admin user. Now delete this file!';
}
else {
echo 'Error with wp_insert_user. No users were created.';
}
}
else {
echo 'This user or email already exists. Nothing was done.';
}
}
else {
echo 'Whoops, looks like you did not set a password, username, or email';
echo 'before running the script. Set these variables and try again.';
}
The JSON API plugin is not needed, as the REST API is part of WordPress core (as of about Version 4.7, if I recall correctly).
You can indeed create a user via WordPress's REST API — you pass a POST
request to the server with the appropriate arguments. The POST
data must include, at a minimum, the user's username, email, and password.
Note that you'll need to be authenticated in order to create a user.
References
- REST API » Create a User
- REST API » Authentication
Caveat: I have not done this myself, but according to the documentation it should be possible.
本文标签: javascriptCreate user from outside WordPress through api
版权声明:本文标题:javascript - Create user from outside WordPress through api? 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745154482a2645085.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论