我想创建一个贡献者privelages一个WordPress用户,远程。不过,我想利用我操纵使用(从登录我的网站上),它们的ID电子邮件和@ example.com还可以创建自己的WordPress的帐户。因此,它将基本上是:id#@example.com。我是很新的节目。我已经找了又找,只是不断地抓我的头。什么是这样做的最佳做法?任何示例,资源,解释将不胜感激!
我想在自己的帐户页面创建一个链接,当登录的用户点击它,它会重定向到,将创建WordPress的框架内,其用户帐户页面。他们将必须登录到我的网站访问。
我不知道该怎么做你的问题的电子邮件部分。虽然,我想知道怎样融合两种在一起。但是,如果你需要为WordPress独特的电子邮件,可以绕过这一要求的部分(如果需要使用WP仪表盘来创建用户)。我最近发现这一点。
你可以简单地说:
$user_name = //however you get your unique identifier this will be their screen name
$user_id = username_exists( $user_name );
if ( !$user_id and email_exists($user_email) == false ) {
$user_id = wp_create_user( $user_name, $user_email );
wp_update_user(array(
'ID' => $user_id,
'role' => 'contributor'
));
}
如果您的用户真正做到让您的服务器上的电子邮件帐户,并且希望他们能够从WordPress的接收电子邮件..这不会解决你的情况。
有很多方法可以做到这一点。最干净的可能会在目前是智威汤逊authentication一起使用WordPress的API。
这里有一个简单的解决方案。在远程WordPress安装你坚持这样的事情到你的functions.php
function remote_create_user() {
$token = (isset($_GET['token'])) ? sanitize_text_field($_GET['token']) : '';
$action = (isset($_GET['action'])) ? sanitize_text_field($_GET['action']) : '';
//this is not particularly secure, but let's assume your wordpress page has https which should encrypt the url...
//im just setting some random string to compare to
if ($token != '712031ff105541219fcc741d99a9addd' || $action != 'createuser') {
return;
}
$username = sanitize_text_field($_GET['username']);
$email = sanitize_text_field($_GET['email']);
//making sure the user doesn already exist
$user_id = username_exists($username);
if (!$user_id and email_exists($email) == false) {
$random_password = wp_generate_password($length = 12, $include_standard_special_chars = false);
//creating the user
$user_id = wp_create_user($username, $random_password, $email);
if ($user_id) {
//here you could send the user a welcome mail. if you want, include the password in the mail or just make him press the "forgot password" button
//wp_mail($to, $subject, $message);
echo "User created";
}
} else {
echo "User already exists.";
}
}
add_action('init', 'remote_create_user');
在系统要发送的命令从,你就可以做这样的事情(在服务器端,你不能这个从浏览器,因为“认证”标志将在JavaScript中可见...
$createuser = file_get_contents('https://yourdomain.com/?token=712031ff105541219fcc741d99a9addd&action=createuser&username=test3&[email protected]');
//depending on the result in $createuser, give an error message or redirect him to your wordpress login page or something
我希望这给你一些想法。
编辑:Aboves初始化函数缺少wp_die()结尾。我们不希望在这里呈现的整个页面。同样,这仅仅是一个快速和肮脏的解决方案。看看WordPress的REST API或还可为客户端点。