我正在尝试设置一个注册系统。
输入字段由javascript检查,如果它们全部有效,则会发送到'/create-user.php。此文件获取用户名,密码,电子邮件,并使用wordpress函数wp_create_user()来创建新用户。不幸的是,它不起作用,似乎wordpress功能无法识别。
创建用户的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
}
?>
这是我使用$ .post获得的数据。我在html元素中显示函数(数据)。请注意在用户回顾之前我是如何得到的。
最后的回声永远不会被执行。
我是新手,但我觉得这个功能没有得到认可。我肯定错过了一些东西。
我可以编写自己的php函数来创建一个新用户,但我想知道为什么内置的wp对我不起作用。
提前感谢你的帮助!
更新:根据wp codex,wp-includes / users.php中可以找到wp_create_user()。我无法在任何地方追踪这个功能。这是有道理的,但为什么在地球上我的wordpress会错过这样的功能?
你把这个包含在该页面的顶部了吗?
require('/wp-blog-header.php');
我想到了。正如Klezper指出的那样,我需要包含wp-blog-header.php,它基本上会加载wordpress。否则,我的php文件无法使用wp函数。我只需要弄清楚文件的确切目录。
我用过这段代码:
<?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 [...]