A question that pops up in the forums from time to time and appeared again just the other day is how do I redirect users to the front page after they log in? In other words the admin didn't want his users being sent to the backend which is the WordPress default behaviour.
I'm sure there are several plugins in the official WordPress Plugin Directory that will accomplish the task although I haven't looked through them to find one. The solution I gave to the original poster, which I'll share with you here, is one that does not involve the use of a plugin.
Add the following to your theme's functions.php file...
/* redirect users to front page after login */
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');
Thank you, but I have tried with your code and it doesn't work. At least with wp 2.8.6
Hi Mau,
I just tested it on a 2.8.6 install and it worked fine. What theme are you using?
Thanks for this post! I have a question though. How can I disable any non-admin user to access the wordpress site and should be able to access it only through the login page? Moreover, is it possible that the admin user would get to see the administration panel and not the site, upon logging in?
Your code above works for redirecting from the login page but the wordpress site is still accessible via localhost/wordpress URL even without logging in. Thanks in advance!
Hi snowivy,
I would take a look at this plugin. It allows you to setup redirects based on specific users, specific roles and specific levels.
Thanks a lot Len!
You're quite welcome.
I tried this today and I get an error:
Fatal error: Call to undefined function add_action() in....
I use the P2 theme.
Any suggestions?
Thanks
Actually after I reread the instructions I put the code in the THEME fuctions.php file and it this time it just gives me a blank screen??
Hi Voogah,
It may be something specific to that theme. I'll download and install it in a local environment and have a look.
Hi Voogah,
I just tested it using WordPress 2.9.2 and the P2 theme and it worked just fine. I inserted the code in the theme's functions.php file right after the bit that registers the sidebar.
To make it easier, you can download a copy of the altered file at the WordPress Pastebin.
Hi,
I just tried the code above (redirect) and it works great--thank you so much! You seem to be very knowledgeable about the WP software and I'm wondering if you might know the answer to a rather bothersome issue: I have the P2 theme; For some reason, the text I type into my search box appears white--so you can't see it because the search box area is white, too. Would you happen to know where one would go to change that text color to black? I'm seeing so many options in the php files that I'm afraid to break something!
Cindy
Hi Cindy,
I just installed that theme on a test site and can't duplicate that problem. I'm not sure why the text in the search form is the colour it is.
One thing you can try. Changes such as this need to be made in the style sheet - a file named
style.css. Look in that file for the following ...#searchform #s {width: 57%;
}
... and add
color: #000000;That block of code should now look like this ...
#searchform #s {width: 57%;
color:#000000;
}
The declaration #000000 will make the text in the search form appear black. You can adjust that to whatever you want.
Hi,
That did the trick! Thank you SO much!
Cindy
You're most welcome.