1

I have created a dynamic link button that links to Buddypress user profile. A login user that clicks on this button will be brought to user's profile. However, a guest that clicks on this button will be by default redirected to WP login page. I want a user to be redirected to my custom login page. How can I do that?

I've tried this code. It works. It redirects to my custom login page. But how can I login to WP as admin in order to go to admin dashboard? As I go to wp-login.php, it will redirect me to my custom page in stead.

add_action('init','possibly_redirect');

function possibly_redirect(){
    global $pagenow;
    if( 'wp-login.php' == $pagenow ) {
        wp_redirect('http://localhost/test/login/');
        exit();
    }
}
2
  • Why can't you login as admin in your custom login page?
    – Johansson
    Commented Aug 18, 2017 at 14:22
  • I can login via custom login page with my admin account but I cannot go to admin dashboard. When go to wp-admin, I will be redirected to wp default login page. I have asked the author of the plugin of the custom login page and he said that it's separated. I cannot go to admin dashboard directly.
    – user125426
    Commented Aug 18, 2017 at 14:51

1 Answer 1

0

There is no reason to redirect the logged-in users. So I would recommend that you add another conditional and redirect the users only if they are not logged in:

add_action('init','possibly_redirect');
function possibly_redirect(){
    global $pagenow;
    if( 'wp-login.php' == $pagenow && ! is_user_logged_in()) {
        wp_redirect('http://localhost/test/login/');
        exit();
    }
}

This way you won't be redirected after login.

3
  • Thanks for your answers but nothing changes, still same.
    – user125426
    Commented Aug 18, 2017 at 16:11
  • If I login with my admin account via my custom login page, I cannot go to wp-admin. If I type in wp-admin at the end of the url, I will be brought to wp-login page to login again. So if I use the code above that I show you, it will replace wp-login.php with my custom login page. As a result, if I type wp-login.php at the end of the url, I will be brought to my cusom login page. My question is how can I login to wp admin dashboard. I hope you get my meaning.
    – user125426
    Commented Aug 18, 2017 at 16:19
  • For other plugins, I can find the file to edit to change to my custom login page link. For example, this text...."You must login to post a comment" By default if I click on the word login in the quotation, I will be brought to default wp login page. But I can change the path as the know where to edit the file. But for Buddypress I don't know where to edit the file.
    – user125426
    Commented Aug 18, 2017 at 16:27

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.