在“Turtletreed”所说的帮助下,我成功地让它工作了。我的代码很好,只是需要更改两件事。我从中删除了自定义字段$user_data, 在后面加上这个wp_insert_user --
add_user_meta($user_id, \'broker_email\', $b_email);
这是我函数中的最终代码。php文件--
add_action(\'template_redirect\', \'register_a_user\');
function register_a_user(){
if(isset($_GET[\'do\']) && $_GET[\'do\'] == \'register\'):
$errors = array();
if(empty($_POST[\'user\']) || empty($_POST[\'email\'])) $errors[] = \'provide a user and email\';
if(!empty($_POST[\'spam\'])) $errors[] = \'gtfo spammer\';
if(!empty($_POST[\'pass1\']) && !empty($_POST[\'pass2\'])) $error[] = \'The passwords you entered do not match\';
$account = esc_attr($_POST[\'account_type\']);
$user_login = esc_attr($_POST[\'user\']);
$user_email = esc_attr($_POST[\'email\']);
$user_pass = esc_attr($_POST[\'pass1\']);
$user_pass2 = esc_attr($_POST[\'pass2\']);
$user_first = $_POST[\'first_name\'];
$user_last = $_POST[\'last_name\'];
$b_email = $_POST[\'broker_email\'];
require_once(ABSPATH.WPINC.\'/registration.php\');
$sanitized_user_login = sanitize_user($user_login);
$user_email = apply_filters(\'user_registration_email\', $user_email);
if(!is_email($user_email)) $errors[] = \'invalid e-mail\';
elseif(email_exists($user_email)) $errors[] = \'this email is already registered\';
if(empty($sanitized_user_login) || !validate_username($user_login)) $errors[] = \'invalid user name\';
elseif(username_exists($sanitized_user_login)) $errors[] = \'user name already exists\';
if(empty($errors)):
if ( $_POST[\'pass1\'] == $_POST[\'pass2\'] ) {
$user_data = array (
\'user_login\' => $sanitized_user_login,
\'user_pass\' => $user_pass,
\'user_email\' => $user_email,
\'user_first\' => $user_first,
\'user_last\' => $user_last,
\'role\' => $account
);
// Create the user
$user_id = wp_insert_user( $user_data );
add_user_meta($user_id, \'broker_email\', $b_email);
} else {
$errors[] = \'passwords dont match\';
}
if(!$user_id):
$errors[] = \'registration failed...\';
else:
wp_new_user_notification($user_id);
endif;
endif;
if(!empty($errors)) define(\'REGISTRATION_ERROR\', serialize($errors));
else define(\'REGISTERED_A_USER\', $user_email);
endif;
}
此外,如果您想让他们自动登录,您可以在之后立即添加此功能--
function auto_login_new_user( $user_id ) {
wp_set_current_user($user_id);
wp_set_auth_cookie($user_id);
// You can change home_url() to the specific URL,such as
wp_redirect( \'http://YOURURL.COM\' );
}
add_action( \'user_register\', \'auto_login_new_user\' );