Login and logout redirection (10 posts)

  1. ninjaboy
    Posted 9 years ago #

    I have had a really good look around this site and many others, and I haven't worked out this issue yet.

    Rather than modify the core code as I have done in the past I am now developing some of my hacks into plugins.

    One such simple function is redirection on login to homepage, which works perfectly fine with the following code bundled into a plugin (credit to WP community, not sure where I got this from!)

    function MCMSlogin_redirect () {
    global $redirect_to;
    if ( ! current_user_can('edit_posts') )
    $redirect_to = get_option('home');
    add_action('wp_authenticate', 'MCMSlogin_redirect');

    So, when wp_authenticate runs it sends the user back to the homepage rather than straight to the dashboard.

    However, I don't seem to be able to do this with logout. I have done some research and have tried using wp_logout - but further research has told me that 'this is an action that is fired when a user is logging out and executes just after wp_clearcookie() runs and before the user is redirected' - which is obviously not going to do what I want!

    I have also had a look at 'loginout' - but can't really find any documentation on this.

    Does anyone have any ideas on how to re-direct on logout?

    I know there are a couple of plugins that do this, but I am really trying hard to learn about WP and would love to see one of you code ninjas come up with a solution to this simple problem I am having!

  2. t0v3
    Posted 9 years ago #

    All i done is change the redirect to


    $redirect_to = 'redirect.php';

    Then in redirect.php i put

    header ("Location: http://mydomain.com/index.php");

  3. ninjaboy
    Posted 9 years ago #

    Humph - I see how this would work but I'd prefer not to have to create another file (eg redirect.php in your example).

    I just need to work out which action to use for the logout redirection variable.

  4. artfulalibi
    Posted 9 years ago #


    I use two plugins working together to acheive a sophisticated login and redirect.
    Redirect Login


    Themed login

    The result is clients can login on my front page, and are simply redirect into the client area of my website, without ever seeing the dashboard!


  5. ninjaboy
    Posted 9 years ago #

    artfulalibi - yup, I have had a look at both of those plugins. Themed login overides some of the core WP finctionality, so I'm not so interested in that one. You can actually achieve something very similar WITHOUT compromising the updates rolled out to WP if you know what you are doing with templates.

    Redirect Login is interesting, but I couldn't get it working right (WP v2.1.3)

    Really I was just trying to understand how to hook into the logout process, just like I have done for the login process in my example above, which I bundled into a simple plugin.

    Simply put - how do I redirect on logout, without hacking the core code. I'm tired of having to apply my hacks everytime a new update of WP is released!

  6. ninjaboy
    Posted 9 years ago #

    ...just to make it clear, I want to bundle this function into a little plugin, rather than use someone elses code, I am trying to lift the lid on these WordPress Actions so I can learn more about the platform and PHP.

  7. ninjaboy
    Posted 9 years ago #

    No clues then on re-directing on logout?? I'm tearing my hair out trying to work this one out!

  8. ninjaboy
    Posted 9 years ago #

    Despite all my experimenting I still have not found out how to do this without modifying the core code. Modify redirect on login - yup, no problem (as shown in the example above) modify redirect on logout - I'd image would have worked the same but I can't for the life of me work it out!!

    This seemed to get resolved (I think) in this:


    I am using WP2.1.3 install for testing this by the way. Why can't I get $redirect_to to work with logout?

  9. MaryJane
    Posted 9 years ago #

    tov3 your solution fixed my logout issues. Thank you so much. You cant even see the redirect page it loads so fast back to the index and im even on dialup!

    I used a combo of the login form i found on here a few bits here and a few there and now i have it when a user logs in the stay on the index page and when they logout they redirect back to the index page. works good for me and no plugin needed.

  10. zuberi
    Posted 8 years ago #

    Yep. I know it was five months ago, but t0v3's solution is absolutely spot on. Perfect, in fact.

    If anyone's reading this who wants a little more info about the solution, here's how I implemented it:

    wrote this file in dreamweaver:

    header ("Location: http://mydomain.com/index.php");

    Saved it as redirect.php and uploaded it to my theme folder (it doesn't matter where you upload it, just as long as you can find it when you need it - see below)

    Then I went to my cPanel and modified the file called


    Only one line needed changing and that was line 78.

    t0v3 recommended changing it to:

    $redirect_to = 'redirect.php';

    But this will only work if you uploaded the file redirect.php to your root folder. I saved it my theme folder, so I had to stipulate the following directories:


    so line 78 looked like this:

    $redirect_to = 'http://www.mydomain.net/wp-content/themes/stitched-10/redirect.php';

    (that should all be on one line.)

    and then it worked!

    No hassle. No messing around in the theme's other .php files. Just pure and simple.

    Thanks t0v3

    Good Work! :)

Topic Closed

This topic has been closed to new replies.

About this Topic