【发布时间】:2014-04-28 00:25:50
【问题描述】:
我正在尝试使用 PHP 将用户添加到 WordPress 中的 wp_users 表。我想我有正确的代码(或者我没有),但我似乎无法让它运行。所以要么我做错了什么,要么我有点困惑(我肯定很困惑)。
基本上我在 WordPress 插件下的 PHP 文件中有这段代码。然后我在 URL 中输入代码:
localhost:8888/xxx/xxx/plugins/register_user.php.
这会起作用还是我需要做什么?当我之后检查 MySQL 时,它没有输入用户。我在做一些公然错误的事情吗?
我以前从未这样做过,也不太了解 PHP,所以请为愚蠢的人解释清楚。这是代码。
<?php # Script 9.3 - register.php
$page_title = 'Register';
add_action('init', 'create_user');
function create_user() {
$username = 'username123';
$password = 'pasword123';
$email = 'drew@example.com';
$user = get_user_by( 'email', $email );
if( ! $user ) {
// Create the new user
$user_id = wp_create_user( $username, $password, $email );
if( is_wp_error( $user_id ) ) {
// examine the error message
echo( "Error: " . $user_id->get_error_message() );
exit;
}
}
}
【问题讨论】: