3

I wonder, how can I create two separate login pages for two specific users?

Say, for example: I have two users on my site. Admin and Viewer.

On my site's frontend I want to create two different login pages. One login form for Admin only and one login form for Viewer only. I want them to be on a different url too.

I hope you could help me with this issue. Thanks!

2
  • 1
    Do you just want to locate/style them or do you want for accounts to be unable to login altogether with the "wrong" form?
    – Rarst
    Commented Oct 14, 2016 at 8:28
  • ah nope. I want to create two login forms. one for admin users and one for viewer users
    – Sayroose
    Commented Oct 17, 2016 at 7:06

4 Answers 4

1

On-topic answer:

You can just put <?php wp_login_form(); ?> into any of your theme templates to render a login form on the front end of your site.

Or make your own shortcode [loginform] by putting this into your theme's functions.php:

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

add_shortcode( 'loginform', 'wpse_242473_login_form' );
1
  • 1
    Hi Andy, how do i specify the login to restrict only Viewer role users can login using the form? I will make two login forms . for admins and for viewers. Thanks
    – Sayroose
    Commented Oct 17, 2016 at 5:48
0

There is the function wp_login_form( [ /* array of arguments */ ] ), which displays a login form that you can put into every template. When you look at the internals, you will see that the forms action points to:

esc_url( site_url( 'wp-login.php', 'login_post' ) )

…(at least if you did not use the $args['redirect'] argument). This means that wp-login.php handles the sign on. Now you have multiple options:

  1. You can use the arguments for the function or globally set the filter login_form_defaults, which allows you to alter the default values, like redirect, which per default points to the current site:

    ( is_ssl() ? 'https://' : 'http://' ) . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'],
    

    Then you can use a filter to redirect based on the role (or perform a log out if the logged in user role did not match the role that you want to allow logging in from this template):

     apply_filters( 'login_redirect', $redirect_to, $requested_redirect_to, $user );
    
  2. You scratch that idea as you can not determine the difference between an admin and a guest as you do not know the role of a user before they logged in.

0

For a custom register user form, you can use Advanced Custom Fields to create a front-end form.

You can set up the custom fields for the registration using ACF, and set the location rules for the field group when User Form is equal to Register, and Add/Edit.

I did something similar, hooking into the acf/pre_save_post filter and doing the following:

  • sanitize the inputs
  • register the user with register_new_user(), which handles the password generation and email notification
  • use wp_update_user() and update_user_meta() to map the appropriate custom ACF fields to custom WP user profile fields
  • set the new user's role
  • use wp_mail() to email admin if required

Some more information on registering a user with ACF here.

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' );
-1

If you are trying to achieve a separate login form on a different page than the one for admins then you can easily use a plugin. There are multiple ones, free or premium.

I will list "Custom Frontend Login Registration Form" as I already worked with it in the past. You can reach it on WordPress Repository.

enter image description here

It will generate a shortcode you can use on a custom page for your Viewer only users. The plugin also comes with registration shortcode, so will make your visitor's life easier.

Hope this answer your question.

1
  • Hi Cristian, I just tried the plugin you said. created the login form using the shortcode. the admin can still login using the form. i want to have the form only for viewers user. i don't see any configurations for the plugin.
    – Sayroose
    Commented Oct 17, 2016 at 5:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.