如何为特定用户创建特定登录页面?

时间:2016-10-14 作者:Sayroose

我想知道,如何为两个特定用户创建两个单独的登录页面?

比方说:我的网站上有两个用户。管理员和查看器。

在我的网站的前端,我想创建两个不同的登录页面。一个登录表单仅用于管理员,一个登录表单仅用于查看器。我希望他们也在不同的url上。

我希望你能在这个问题上帮助我。谢谢

4 个回复
SO网友:Andy Macaulay-Brook

主题答案:

你可以把<?php wp_login_form(); ?> 进入任何主题模板,在站点前端呈现登录表单。

或者制作自己的短代码[loginform] 通过将其纳入主题functions.php:

function wpse_242473_login_form() {
    return wp_login_form( \'echo\' => false );
}

add_shortcode( \'loginform\', \'wpse_242473_login_form\' );

SO网友:kaiser

有功能wp_login_form( [ /* array of arguments */ ] ), 它显示一个登录表单,您可以将其放入每个模板中。当您查看内部构件时,您将看到表单操作指向:

esc_url( site_url( \'wp-login.php\', \'login_post\' ) )
…(至少如果您没有使用$args[\'redirect\'] 参数)。这意味着wp-login.php 处理登录。现在您有多种选择:

您可以使用函数的参数或全局设置过滤器login_form_defaults, 允许您更改默认值,如redirect, 默认情况下,哪些指向当前站点:

( is_ssl() ? \'https://\' : \'http://\' ) . $_SERVER[\'HTTP_HOST\'] . $_SERVER[\'REQUEST_URI\'],
然后,您可以使用筛选器根据角色重定向(如果登录的用户角色与您希望允许从此模板登录的角色不匹配,则执行注销):

 apply_filters( \'login_redirect\', $redirect_to, $requested_redirect_to, $user );
因为在用户登录之前,您不知道用户的角色,所以无法确定管理员和来宾之间的区别,所以您不知道这个想法。

SO网友:davemac

对于自定义注册用户表单,可以使用Advanced Custom Fields to create a front-end form.

您可以使用ACF设置用于注册的自定义字段,并在用户窗体等于Register时设置字段组的位置规则,以及添加/编辑。

我也做了类似的事情acf/pre_save_post 过滤并执行以下操作:

清理输入,向用户注册register_new_user(), 用于处理密码生成和电子邮件通知wp_update_user()update_user_meta() 要将适当的自定义ACF字段映射到自定义WP用户配置文件字段,请设置新用户的角色wp_mail() 如有需要,请发送电子邮件给管理员registering a user with ACF 在这里

function my_pre_save_user( $post_id ) { 

    // If we have an email address, add the user
    // This field only exists in the new user field group
   if ( isset($_POST[\'acf\'][\'add acf_field_ID here\']) && !empty($_POST[\'acf\'][\'add acf_field_ID here\'])) {

        // sanitize our inputs
        $sanitized_email = sanitize_email( $_POST[\'acf\'][\'add acf_field_ID here\'] );
        $sanitized_firstname = sanitize_text_field( $_POST[\'acf\'][\'add acf_field_ID here\'] );
        $sanitized_lastname = sanitize_text_field( $_POST[\'acf\'][\'add acf_field_ID here\'] );
        $sanitized_contactnumber = sanitize_text_field( $_POST[\'acf\'][\'add acf_field_ID here\'] );

        // Prepare basic user info
        $username = $sanitized_email;
        $email = $sanitized_email;
        $display_name = $sanitized_firstname .\' \'. $sanitized_lastname;

         // Register the user and store the ID as $user_id, handles the validation, generates random password and send email notification to user
        $user_id = register_new_user( $username, $email );

        // If we get an error (eg user already exists)
        if( is_wp_error( $user_id ) ) {
            // Show the error
            echo \'Error: \'.$user_id->get_error_message();
            // Exit
            exit;

        // Good to go
        } else {

            // get single value from post object
            $dmc_get_company_field = $_POST[\'acf\'][\'add acf_field_ID here\'];
            $dmc_selected_exhibitor = get_field( $dmc_get_company_field );

            // Update the new user\'s info
            wp_update_user( array(
                \'ID\' => $user_id,
                \'first_name\'  => $sanitized_firstname,
                \'last_name\'  => $sanitized_lastname,
                \'display_name\'  => $display_name
            ));

            // update the new users\'s meta
            update_user_meta( $user_id, \'dmc_exhibitor_company_name\', $dmc_get_company_field  );
            update_user_meta( $user_id, \'dmc_exhibitor_contact_number\', $sanitized_contactnumber );

            // update user role
            $user_id_role = new WP_User( $user_id );
            $user_id_role->set_role( \'contributor\' );
            $profile_link = get_edit_user_link( $user_id );

            $to = "[add email addresses here]";
            $headers[] = \'MIME-Version: 1.0\';
            $headers[] = \'Content-Type: text/html; charset=UTF-8\';
            $headers[] = \'Reply-To: \'. $username. \' <\'. $email .\'>\';

            $subject = "[add email subject here]";
            $body = "[add email body here]";

            // send the email
            wp_mail( $to, $subject, $body, $headers );

            // redirect to thankyou page
            $redirect_url = get_bloginfo(\'url\') . \'[add URL to redirect to here]\';
            wp_redirect( $redirect_url );

            // exit this script
            exit;

        }

    } else {
        return $post_id;
    }

}
add_filter(\'acf/pre_save_post\' , \'my_pre_save_user\' );

SO网友:Cristian Stan

如果您试图在不同于管理员的页面上实现单独的登录表单,那么您可以轻松使用插件。有多个,免费或高级。

我将列出“Custom Frontend Login Registration Form“因为我以前已经使用过它。您可以在WordPress存储库中访问它。

enter image description here

它将生成一个快捷码,您可以在自定义页面上为您的Viewer only 用户。该插件还附带注册快捷码,因此将使访问者的生活更轻松。

希望这能回答你的问题。

相关推荐

PHP警告-在wp-config.php上使用未定义的常量‘force_ssl_login’‘force_ssl_admin’

我有一个困扰多年的PHP警告,我想知道是否有人能解决这个问题。以下两行警告不断记录在my error\\u日志文件中:PHP Warning: Use of undefined constant ‘FORCE_SSL_LOGIN’ - assumed \'‘FORCE_SSL_LOGIN’\' (this will throw an Error in a future version of PHP) in /home/myuser/public_html/wp-config.php on line 92