WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] redirect after user login (24 posts)

  1. Anonymous
    Unregistered
    Posted 5 years ago #

    I'm using wordpress 2.6 to manage a website for a novel I've written. I have it set up so that the main site is a static page instead of the default chronological list of posts.

    I want to set up the site so that users are redirected to the main page after they log in. I don't want them going to the dashboard or writing screen or profile screen. They should go straight to the site's home page.

    Unfortunately, I don't write code as well as I write books! I searched the forums before I posted this question, and I found this:

    <?php
    /*
    Plugin Name: Redirect user to front page
    Plugin URI: http://www.theblog.ca/wplogin-front-page
    Description: When a user logs in via wp-login directly, redirect them to the front page.
    Author: Peter
    Version: 1.0
    Author URI: http://www.theblog.ca
    */
    
    function redirect_to_front_page() {
    	global $redirect_to;
    	if (!isset($_GET['redirect_to'])) {
    		$redirect_to = get_option('siteurl');
    	}
    }
    
    add_action('login_form', 'redirect_to_front_page');
    ?>

    It sounded like it was what I needed, but I couldn't get it to work. I put my site url in the right place, but i got a blank screen when I activated it and tried it.

    Does anyone have any suggestions ?

  2. flick
    Member
    Posted 5 years ago #

    For info (in case it wasn't obvious,sorry ) this is the site that provided the plugin.

    If you wanted to redirect to the front page of your site, you won't actually need to change anything. Just activate the plugin and go.

    This is because it is redirecting to the value of 'siteurl' in your wp_options table, which is otherwise your actual site url, or http://www.yoursite.com.

    Hope this helps.

    p/s: This looks like a potentially promising plugin as well, but not sure if it works in 2.6; From Where you Came

  3. Anonymous
    Unregistered
    Posted 5 years ago #

    First, thanks for supplying the link to the author's site. I thought I included it before the code, but I must have screwed up the tags.

    (obviously, I'm not a coder.)

    Secondly, thanks for the explanation. I feel kinda dumb for not seeing that 'siteurl' was a variable, but my embarrassment is eased somewhat because it does work like a charm not that I've quit trying to make things more complicated than they have to be.

  4. flick
    Member
    Posted 5 years ago #

    @tommy2guns: Yup, you did supply the link. I just thought it would be useful for others when/if they reference this thread (when they search the forums too :P) to have an extra link to visit.

    Actually, I find the question very helpful, so thank you as well. It took me a while to work out how to modify the url to a custom page on the site, so that is what I learnt from it :)

  5. Anonymous
    Unregistered
    Posted 5 years ago #

    Even though you solved your problem, I still feel I have to inform people about a much more...how do I put this...a much more no-need-for-plugin-ish way to do this (excuse that ridiculos word :D).
    Heres all you need to do (or at least this worked for me):
    Open your wordpress/wp-login.php file, and find this line:
    $redirect_to = admin_url();

    and replace it with this:

    $redirect_to = get_option('siteurl');

    There you go. A plugin might be much more comfortable to use and it might be a more convenient and easy and straightforward solution, but hey, use this if your plugin causes any kind of conflict or if you like to code yourself :D.

  6. flick
    Member
    Posted 5 years ago #

    Thanks jacf182 :) This is useful information!

  7. Anonymous
    Unregistered
    Posted 5 years ago #

    How do I get the username variable they used to login with? I'd like to make an IF statement so if the username equals something, it forwards them to the dashboard, anything else will redirect them to the homepage.

  8. EvanWatkins
    Member
    Posted 5 years ago #

    how do I redirect to a custom page (not the home page and not the admin page, another page that may even be outside of the WP environment)

  9. Anonymous
    Unregistered
    Posted 5 years ago #

    change:
    $redirect_to = get_option('siteurl');

    to:
    $redirect_to = 'http://www.newsite.com';

  10. EvanWatkins
    Member
    Posted 5 years ago #

    that does not worrk Berryrag, it still redirects me to the admin page, I have even tried changing it in wp-login to redirect to another page and nothing happens. I need this fixed asap please help me

  11. EvanWatkins
    Member
    Posted 5 years ago #

    here is my tester code.....

    <?php
    /*
    Plugin Name: Redirect user to front page
    Plugin URI: http://www.theblog.ca/wplogin-front-page
    Description: When a user logs in via wp-login.php directly, redirect them to the front page.
    Author: Peter
    Version: 1.0
    Author URI: http://www.theblog.ca
    */
    
    function redirect_to_front_page() {
    	global $redirect_to;
    	if (!isset($_GET['redirect_to'])) {
    		$redirect_to = 'http://www.google.com';
    	}
    }
    
    add_action('login_form', 'redirect_to_front_page');
    ?>

    and when I login via wp-login it sends me to admin, whats wrong?

  12. Anonymous
    Unregistered
    Posted 5 years ago #

    Did you try disabling the plugin, and opening the wordpress/wp-login.php

    Find the line: "$redirect_to = admin_url();"

    and replace it with this: "$redirect_to = 'http://www.google.com';"

  13. EvanWatkins
    Member
    Posted 5 years ago #

    no luck it still takes me to the dashboard

    and mine says $redirect_to = 'wp-admin/'; not admin_url();

  14. EvanWatkins
    Member
    Posted 5 years ago #

    any ideas?

  15. Anonymous
    Unregistered
    Posted 5 years ago #

    What version of WordPress are you using?

  16. EvanWatkins
    Member
    Posted 5 years ago #

    2.5.1

  17. Anonymous
    Unregistered
    Posted 5 years ago #

    Maybe try upgrading to 2.6?

  18. garethooper
    Member
    Posted 5 years ago #

    Although this works for me in terms of the admin url change to siteurl, there is no differentiator between an Admin and a subscriber, it takes me to the home page regardless. I need the admin to goto the Admin and a Subscriber goto Home. Or is what I am asking handled by a Plugin? Preferred Plugin that has been used out there?

  19. bitethemailman
    Member
    Posted 5 years ago #

    Here's what you can do in wp-login.php (2.5)

    Change line 392 to the following:

    <input type="hidden" name="redirect_to" value="<?php echo get_bloginfo('home'); ?>" />
  20. smoothzhuhai
    Member
    Posted 5 years ago #

    I had the same issue as tommy2guns from the beginning (2.5).

    What worked for me was to modify wp-login.php in case 'logout' :
    `
    wp_logout();

    //---add this---
    $redirect_to = get_bloginfo('home');

    wp_safe_redirect($redirect_to);
    exit();

  21. seclitco
    Member
    Posted 5 years ago #

    Can the wp-register requirement be created so that it only applies to posts and pages, but not to the home page? I'd like people to be able to reach the home page so they can see the posts, etc. before they are required to register when they click on a post,page, etc.

  22. valuxes
    Member
    Posted 5 years ago #

    This is so ridiculous (frustration level growing... ;o)

    In wp-login.php:

    - find where it says:

    case 'login' :
    default:
    if ( isset( $_REQUEST['redirect_to'] ) )
    $redirect_to = $_REQUEST['redirect_to'];
    else
    $redirect_to = admin_url();

    - Change:

    $redirect_to = admin_url();

    to:

    $redirect_to = site_url();

    --------

    Before you do this, ensure your theme provides a link on your home page to login/logout and register/site admin.

  23. afge1106
    Member
    Posted 5 years ago #

    Is it possible to accomplish this in the functions.php file so that it will survive an upgrade?

  24. christykny
    Member
    Posted 5 years ago #

    Good question, afge1106. Since the last post was a month ago and the question marked as Resolved. You may get better response if you start a new thread and direct an explanation to this thread.

Topic Closed

This topic has been closed to new replies.

About this Topic