0

I am working on matrimonial site I have created login and registeration form for user. And for storing info I have created custom tables. In my login form there are two fields EmailID and password. For other user I can check if user is registerd. But using same form how can I check user is admin? There is problem on password field. (After successful login I want admin to redirect to some pages likes report and all) So the dashboard login and this login have to be seperate

0

Why in the first place you created custom tables? There is wp_usermeta table for storing any kind of extra data to build custom user profiles with extra fields.

Also you can hook into native WP login process and allow logging in with email.

What method of logging in you are using?

Checking if user is administrator:

if ( current_user_can( 'manage_options' ) ) {
/* A user with admin privileges */
    wp_redirect( 'YOUR_TARGET_ABSOLUTE_URL' );
    exit;
}

But there's even better way, as described in Codex:

function my_login_redirect( $redirect_to, $request, $user ) {
//is there a user to check?
global $user;
if ( isset( $user->roles ) && is_array( $user->roles ) ) {
    //check for admins
    if ( in_array( 'administrator', $user->roles ) ) {
        // redirect them to the default place
        return $redirect_to;
    } else {
        return home_url();
    }
} else {
    return $redirect_to;
}
}

add_filter( 'login_redirect', 'my_login_redirect', 10, 3 );

The function is pretty self explanatory. Change $redirect_to destination to one of your choice.

  • Above code is good for redirecting But now how shoul I check user is logged in Which information is stored in custom table – bhagyashree Sep 25 '14 at 9:03
  • That's why I asked "Why in the first place you created custom tables?". Because if you store users (not only usermeta, but user logins and passwords) in your own custom table, you can't use native WP login functions, and the function is_user_logged_in() will not help you. But if you use native WP user table and registration/login process, with your own custom form and custom desing, on a custom page - then that is the function to use. See codex.wordpress.org/Function_Reference/is_user_logged_in – Ihor Vorotnov Sep 25 '14 at 11:29
  • I am getting but I have huge data.And I have no option than creating custom table. – bhagyashree Sep 26 '14 at 4:31
  • I'm working on a large wp multisite project right now with 65k already registered users and the numbers will grow to millions. We have custom registration, login, lostpassword etc (actually, all of them are just customized via actions and filters native wp features). Every user has its own 'profile' custom post type post with lots of usermeta, relationships to another post type objects and taxonomies. Everything works fine with native wp APIs because we are making proper use of all the core features wp has to offer. – Ihor Vorotnov Sep 26 '14 at 8:16
  • The only problem with wp native usermeta table is that it has no indexes on meta values. So, quering users by custom meta values will do full table scans. But instead of creating completely custom tables we are working on an additional table with indexes, spinning up the concept of 'horizontal meta' proposed by horizontalmeta.com plugin. – Ihor Vorotnov Sep 26 '14 at 8:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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