我试图创建一个自定义用户,然后以编程方式登录,然后我想将他们重定向到付款页面。
我的主题函数中有以下代码。php文件:
function sp_registration_process_hook() {
if (isset($_POST[\'adduser\']) && isset($_POST[\'add-nonce\']) && wp_verify_nonce($_POST[\'add-nonce\'], \'add-user\')) {
// die if the nonce fails
if ( !wp_verify_nonce($_POST[\'add-nonce\'],\'add-user\') ) {
wp_die(\'Sorry! That was secure, guess you\\\'re cheatin huh!\');
} else {
//create a new role
remove_role( \'service_provider\' );
$result = add_role(
\'service_provider\',
__( \'Service Provider\' ),
array(
\'read\' => true, // true allows this capability
\'edit_posts\' => true,
\'delete_posts\' => true, // Use false to explicitly deny
\'level_0\' => true,
\'level_1\' => true
)
);
// auto generate a password
$user_pass = wp_generate_password();
echo $user_pass;
$user_login = esc_attr( $_POST[\'user_name\'] );
$user_email = esc_attr( $_POST[\'email\'] );
// setup some error checks
if ( !$user_login ) {
$error = \'A username is required for registration.\';
}
elseif ( username_exists($user_login) ) {
$error = \'Sorry, that username already exists!\';
}
elseif ( !is_email($user_email, true) ) {
$error = \'You must enter a valid email address.\';
}
elseif ( email_exists($user_email) ) {
$error = \'Sorry, that email address is already used!\';
}
// setup new users and send notification
else
{
$user_id = wp_create_user($user_email, $user_pass, $user_email);
wp_update_user(
array(
\'ID\' => $user_id,
\'nickname\' => $user_email
)
);
$user = new WP_User($user_id);
$user->set_role(\'service_provider\');
custom_login( $user_email, $user_pass );
}
}
}
}
add_action(\'process_sp_registration_form\', \'sp_registration_process_hook\');
function custom_login($email, $pass) {
$creds = array();
$creds[\'user_login\'] = $email;
$creds[\'user_password\'] = $pass;
$creds[\'remember\'] = true;
$user = wp_signon( $creds, false );
if ( is_wp_error($user) )
echo $user->get_error_message();
}
// run it before the headers and cookies are sent
add_action( \'after_setup_theme\', \'custom_login\' );
用户创建得很好,但不会自动登录用户。当我查看前端时,用户在发布后肯定没有登录。
你能看出我做错了什么吗?任何帮助都将不胜感激。
雅克