I'm trying to setup a register system.
Input fields are checked by javascript and if they're all valid they are sent to '/create-user.php. This file gets post username, password, email and uses wordpress function wp_create_user() to create the new user. Unfortunately, it doesn't work, it seems like the wordpress function doesn't get recognised.
Create-user-php:
<?php
if(isset($_POST['username']) && !empty($_POST['username']) AND
isset($_POST['email']) && !empty($_POST['email']) AND
isset($_POST['password']) && !empty($_POST['password']))
{
$username = $_POST['username'];
$email = $_POST['email'];
$password = $_POST['password'];
echo "account to be created: <br>" .
"username: " . $username . "<br>" .
"email: " . $email . "<br>" .
"password: " . $password; //this is just a way for me to check if the post info is sent correctly
echo "Now wp_create_user() will be used to create the new user...";
$user_id = wp_create_user( $username, $password, $email );
echo "Newly created user ID is: " . $user_id; //No complicated error check to keep the code simple for stackoverflow
}
?>
And this is the data I get back by using $.post. I display the function (data) inside an html element. Notice how I only get something up until the user recap.
The last echo never gets executed.
I'm a newbie, but I think that function doesn't get recognized..I am surely missing something.
I could write my own php function to create a new user, but I would like to find out why wp built in one isn't working for me.
Thanks in advance for your assistance!
Update: according to wp codex, wp_create_user() is to be found inside wp-includes/users.php. I could not track down that function anywhere. It would make sense, but why on Earth would my wordpress miss such a function?
Did you put this include in the top of that page?
require('/wp-blog-header.php');
I figured it out. As Klezper pointed out, I needed to include wp-blog-header.php, which basically loads wordpress. Otherwise, my php file could not use wp functions. I just needed to figure out the exact directory of the file.
I have used this code:
<?php
$wpInclude = '/wp-blog-header.php'; //Required wordpress file to use wp functions
$serverRoot = ($_SERVER['DOCUMENT_ROOT']); //Root server dir where wordpress is installed
define('WP_USE_THEMES', false);
require($serverRoot . $wpInclude); //requires the file 'wordpressdirectory/wp-blog-header.php'
//whatever you need to do [...]