In this video I am showing you how to create PHP scripts that will help you create a new WordPress user and then check if WordPress user with provided user name and password exists. You can use these scripts to make your app store user details in WordPress users table and let WordPress Users login with same credentials into your mobile app.
Register New User Example PHP script
/*
* Template Name: Register New User Example
*/
$user_name = htmlentities($_REQUEST[“user_name”]);$user_password = htmlentities($_REQUEST[“user_password”]);
$user_email = htmlentities($_REQUEST[“user_email”]);
$user_url = htmlentities($_REQUEST[“user_url”]);
$display_name = htmlentities($_REQUEST[“display_name”]);
$role = ‘author’;if(empty($user_name) || empty($user_password) || empty($user_email) || empty($display_name))
{
$returnValue = array(“Status”=>”error”,”Message”=>”Missing required field”);
echo json_encode($returnValue);
return;
}
if ( username_exists( $user_name ) )
{
$returnValue = array(“Status”=>”error”,”Message”=>”User exists”);
echo json_encode($returnValue);
return;
}
//$user_password = wp_hash_password($user_password);
$userdata = array(
‘user_login’ => $user_name,
‘user_url’ => $user_url,
‘user_pass’ => $user_password,
‘user_email’ => $user_email,
‘display_name’ => $display_name,
‘role’ => $role
);
$user_id = wp_insert_user( $userdata ) ;
$user = new WP_User($user_id);
$user->set_role($role);
if(!empty($user_id))
{
$returnValue = array(“Status”=>”ok”,”Message”=>”Subscriber created”);
echo json_encode($returnValue);
}
/*
$user_password = wp_hash_password($user_password);
global $wpdb;
$insertUser = “INSERT INTO $wpdb->users (user_login, user_pass, user_email, user_url, display_name) VALUES (‘$user_name’, ‘$user_password’, ‘$user_email’, ‘$user_url’)”;
$wpdb->query( $insertUser );
$user_id = $wpdb->get_var($wpdb->prepare(“SELECT id FROM $wpdb->users WHERE user_email = %s”, $user_email));
$my_user = new WP_User( $user_id );
$my_user->set_role( “author” );
*/
?>
Check user name and password PHP Script
/*
* Template Name: Check user name and password
*/
$user_name = htmlentities($_REQUEST[“user_name”]);
$user_password = htmlentities($_REQUEST[“user_password”]);
if(empty($user_name) || empty($user_password) )
{
$returnValue = array(“Status”=>”error”,”Message”=>”Missing required field”);
echo json_encode($returnValue);
return;
}
$user = get_user_by( ‘login’, $user_name );
if ( $user && wp_check_password( $user_password, $user->data->user_pass, $user->ID) )
{
$returnValue = array(“Status”=>”ok”,”UserStatus”=>”1″,”Message”=>”User exists”);
echo json_encode($returnValue);
}
else
{
$returnValue = array(“Status”=>”ok”,”UserStatus”=>”0″,”Message”=>”User is not found”);
echo json_encode($returnValue);
}
?>
To learn how to create user interface for Register/Sign up and Login functionality with Swift please watch videos below: