An external script from a different url will send a curl post with user information such as (username, password, first name, last name, etc) to a script located somewhere in my wordpress installation in order to create the user.
So far I have this but I think it is not working:
<?php
require_once('wp-includes/registration.php');
$user_name = $_POST["username"];
$user_email = $_POST["email"];
$user_id = username_exists( $user_name );
if ( !$user_id ) {
$random_password = wp_generate_password( 12, false );
$user_id = wp_create_user( $user_name, $random_password, $user_email );
} else {
$random_password = __('User already exists. Password inherited.');
}
?>
1) What would be the basic skeleton of this script to receive a post and insert the user into the database? Like what files should it “require_once”?
2) Where would it be best to locate this script in the wordpress installation? (I will need to provide the external script with the script url so they can post the users’ info)
Please help
For if anybody needs it, including
wp-load.php
at the begining of my script, worked for me. Assuming the script is located in the root WordPress directory, the line of code to includewp-load.php
would be this:Now I can use all WordPress functions.
Cheers!
I don’t think you need to require anything. Put it in a plugin file:
https://codex.wordpress.org/Writing_a_Plugin
Then put your code within an “action hook”. Init should work.
https://codex.wordpress.org/Plugin_API/Action_Reference
Also, depending on what you’re doing [for instance, can only users who are not logged in see your form? Because a logged in user entering values here will cause an error], consider using different names for your custom variables, like “my_email” instead of “email”.