自定义用户创建和自动登录问题

时间:2015-07-26 作者:Jacques

我试图创建一个自定义用户,然后以编程方式登录,然后我想将他们重定向到付款页面。

我的主题函数中有以下代码。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\' );
用户创建得很好,但不会自动登录用户。当我查看前端时,用户在发布后肯定没有登录。

你能看出我做错了什么吗?任何帮助都将不胜感激。

雅克

1 个回复
SO网友:Sisir

我的一个应用程序上有类似的功能。我用过wp_set_auth_cookie().

删除custom_login() 功能和用途wp_set_auth_cookie($user_id) 取而代之。

结束

相关推荐

Authentication/API Questions

我们正在尝试使用S2Member来管理我们的WP站点和我们正在启动的Flash程序的成员资格。我们希望在用户登录并保持登录状态时使用S2Member对其进行身份验证。问题:Java层需要知道哪个用户登录到Wordpress(如果有的话),以便它可以将正确的数据返回到flash层。建议的解决方案:Wordpress的cookie也应该包含在Flash到Java的请求中。Java可以使用这些cookie来查询Wordpress DB以获取用户ID。我们需要知道的是:1。是否可以从数据库或文件系统获取带有coo