0

I have created custom user roles 'teacher' and 'student' using members plugin. But they are unable to log in using wp_signon but the admin and other roles are able to log in. I tried using my custom login page and wp-admin. It doesn't work.

function get_username($email) {
  $user = get_user_by('email', $email);
  return $user->data->user_login;
}

function ajax_login(){

  // First check the nonce, if it fails the function will break
  check_ajax_referer('ajax-login-nonce', 'security');

  $login = get_username($_POST['email']);
  $pass = $_POST['password'];

  // Nonce is checked, get the POST data and sign user on
  $info = array();
  $info['user_login'] = $login;
  $info['user_password'] = $_POST['password'];
  $info['remember'] = true;

  $user_signon = wp_signon( $info, false );
  if ( is_wp_error($user_signon) ){
    echo json_encode(array('loggedin'=>false, 'message'=>__('Wrong username or password.'), 'login'=>$login, 'pass'=>$pass));
  } else {
      echo json_encode(array('loggedin'=>true, 'message'=>__('Login successful, redirecting...')));
  }

  die();
}

// Enable the user with no privileges to run ajax_login() in AJAX
add_action( 'wp_ajax_nopriv_ajaxlogin', 'ajax_login' );

Can anybody help me?

  • What doesn’t work? They can’t access wp-admin? They receive an error message? – Jacob Peattie Apr 20 at 14:43
  • They can access wp-admin but can't log in. It keeps saying "wrong password" although the password is correct. I tried to log in admin and subscriber and that works. – iDev Apr 20 at 15:05

Your Answer

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

Browse other questions tagged or ask your own question.