WordPress.org

Support

Support » Plugins and Hacks » Redirect to blog after login

Redirect to blog after login

  • Hi. I’m trying to figure out how to get the login process to redirect directly to the blog after a registered user logs in. I’ve tried a couple of different things in wp-login.php, but they usually break the process, and never actually let me get from the blog to the login page. I’ve been reading the forums, and haven’t seen anything about this. Is there a hack or mod (or an idea) for how to do this that I have not seen? Thanks…

Viewing 13 replies - 1 through 13 (of 13 total)
  • A problem you could face with redirecting folks to the index.php page after the login is that there is no link on the plain vanilla index.php to the admin interface page.

    See my revision to my original post: it’s line 301, not line 95.
    Line 301 is actually the “then” part of an if..then clause. Here’s the if part, just above line 301:

    <?php if (isset($HTTP_GET_VARS["redirect_to"])) { ?>
    <input type="hidden" name="redirect_to" value="<?php echo $HTTP_GET_VARS["redirect_to"] ?>" />
    <?php } else { ?>
    <input type="hidden" name="redirect_to" value="wp-admin/" />
    <?php } ?>

    When I changed line 301 to redirect to index.php instead of wp-admin, my index’s “login” link took me to the admin page.

    Skippy, thank you! That is absolutely what I was looking to make happen! I guess I missed that part of the code when I was looking through. Thanks again!
    2fargon, you’re correct – the “logged in” user won’t be able to post, but that is exactly what I am looking for with this particular blog. This one is my ramblings, just a personal/family informational kind of thing.
    Thanks again for the help!

    Is that line 301 of root/wp-login.php, Skippy? My file only goes to line 254 and I don’t want to put the code in the wrong place. I’m running WP 1.5.1.3.

    chrissearle
    Member

    @chrissearle

    In 1.5.2 wp-login.php reads

    $redirect_to = ‘wp-admin/’;

    I changed it to

    $redirect_to = ‘/’;

    since my WP install is installed on the web-root and this works fine.

    From reading the code – setting the request parameter “redirect_to” from the login link should work without editing the wp-login file (so I thought maybe we could do this in the template instead of editing core wp files). However – the links I have are created by the wp_loginout function which takes no parameters – so I don’t think it can easily be set.

    chrissearle
    Member

    @chrissearle

    Oops – forgot the line number – in mine – wp-login.php line 165

    theillustratedlife
    Member

    @theillustratedlife

    To get mine to work, I had to change this in wp-login.php:

    if ($user_login && $user_pass) {
    $user = get_userdatabylogin($user_login);
    if ( 0 == $user->user_level )
    $redirect_to = get_settings(‘siteurl’) . ‘/’;

    That last line used to redirect to /wp-admin/profile.php. This way, if a reader logs in, they get the main site. If I log in, I get the admin backend. If they need to change their profile, they can still clikc the Admin link after they login to get back to the backend.

    Not bad for someone who doesn’t know shit about php. =)

    RavanH
    Participant

    @ravanh

    Tip for anyone who can use this:

    On WP2 I use the following methode to redirect users back to the same page they came from. So not coursly to the indexpage when logging in from a subpage, category page or post.

    Instead of using the standard <?php wp_loginout(); ?> in the sidebar.php of my theme, I defined the following bit that dispayes a standard text for visitors-not-logged and some links for logged-users:


    <h3>My Menu</h3>
    <?php global $user_identity, $user_level; get_currentuserinfo(); if (!$user_identity) { ?>
    <!-- put text for not logged visitors here -->
    <a href="<?php bloginfo('url'); ?>/wp-login.php?redirect_to=<?php echo get_permalink(); ?>">Login</a>
    <?php ; } else { printf(__('<p class="searchinfo">Welcome, <strong>%s</strong>.
    '), $user_identity); ?>
    <a href="<?php bloginfo('url'); ?>/wp-admin/profile.php">My Profile</a>
    <a href="<?php bloginfo('url'); ?>/wp-admin/profile.php?page=subscribe2/subscribe2.php">My Subscribtions</a> <!-- link to Subscribe2 plugin by Skippy -->
    <?php if ($user_level > 1) { wp_register(); }?>
    <a href="<?php bloginfo('url'); ?>/wp-login.php?action=logout&amp;redirect_to=<?php echo get_permalink(); ?>">Logout</a>
    <?php } ?>

    It needs adaptation for any perticular theme (like putting in unordered list and list item tags) but this might give some an idea about what can be done…

    RavanH, thanks a lot for sharing your solution. I’ve looking for it several hours.

    Here’s the code I added to redirect users to the homepage as well:

    if (is_home()) {
    $redir = get_settings('home');
    } else {
    $redir = get_permalink();
    }
    if (!$user_identity) { ?>
    <!-- put text for not logged visitors here -->
    <a href="<?php bloginfo('url'); ?>/wp-login.php?redirect_to = echo $redir; ?>">Login</a>

    etc.

    Is there any way to direct users to the Admin > Write > Post page rather than Admin > Dashboard after they log in?

    After some research, and just in case someone is interested, I’ve improved my code by changing the first part from this:

    if (is_home()) {
    $redir = get_settings('home');
    } else {
    $redir = get_permalink();
    }

    to this:

    if ((is_home()) && !($_SERVER['QUERY_STRING'])) {
    $redir= get_settings('home');
    } else {
    $redir= get_settings('siteurl') . $_SERVER['REQUEST_URI'];
    }

    As an addition to chrissearle’s info since I don’t want my users to see the login screen unless they are trying to login, I changed my WP 2.0.1 wp-login.php thusly:

    line 28: $redirect_to = ‘/’;
    line 239: <input type=”hidden” name=”redirect_to” value=”/” />

    Users are always redirected to the home page after login and logout.

    firesnake
    Member

    @firesnake

    I just used find and searched for $redirect_to in wp-login.php and replaced every instance of wp-admin with index.php. (My index.php is in my wordpress root.)

    It doesn’t seem broken yet.

Viewing 13 replies - 1 through 13 (of 13 total)
  • The topic ‘Redirect to blog after login’ is closed to new replies.