WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'm making a member-only site that requires users to be logged in to use the site.

The homepage is publicly viewable. On the homepage, I'd like to put in a function to redirect users to a page inside the site if they are logged in.

The code I tried

<?php if (is_user_logged_in() ) {
 wp_redirect ('dashboard' );

Gives me a "headers already sent" error. The code is placed at the top of my php file.

What am I doing wrong?

share|improve this question
Does it say where the headers ate sent from? – Bainternet Jul 24 '11 at 16:27
/wp-includes/pluggable.php on line 934 – javy Jul 24 '11 at 16:38
try turning on wp_debug and see whats going on there – Bainternet Jul 24 '11 at 17:57

I am not sure if you just simplified for example, but make sure you use full URL in wp_redirect() call.

Also rather than having it in template, I think it would be more appropriate to run this at template_redirect hook.

share|improve this answer
My solution is wp_redirect ( home_url("/dashboard") );, and I've put this code on line 1 of the template. Although I'm not familiar with hooks so you probably have a valid point. Why? – javy Jul 24 '11 at 18:36
@javy template_redirect is semantically place in code like "WP knows what it needs to start loading and outputting, but you can change its mind". Start of template is "we are already loading this thing". If you don't want what's in template there is no reason to get that far in the process. – Rarst Jul 24 '11 at 18:53
Makes sense, sounds like it saves some server utilization? – javy Jul 25 '11 at 0:14
@javy I don't think difference in performance would be meaningful between too. – Rarst Jul 25 '11 at 7:28
<?php if (is_user_logged_in() ) {
wp_redirect ( home_url("/dashboard") );

Is what I've come up with, and works. Also, I think I also messed up by putting this below

/*Template Name: Home*/

The new code is on line 1 and works.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.