Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

WordPress

Deleted User

How to Redirect Logged In User to Blog Page in Wordpress

Hi,

I am in the finishing stages of building a 'blog-like' Wordpress website and feel fairly comfortable with the general php coding, however I'm really stuck on this one problem.

I'm trying to implement what seems like a simple redirect task, where I have a home/landing page located at the root url of my site (http://example.com) which has information about the site and links to sign up and log in. My intention is to make this the "default" page that users end up at if not logged in.

Aside from the landing page, I have a page located at '/blog', where I would like users to be redirected to upon login (this part is working with the help of the "Peter's Login Redirect" plugin) and automatically redirected to if still logged in. So instead of a user always having to navigate from the homepage, and THEN to the '/blog', the site would recognize that the user is logged in and redirect them to the '/blog' page (until they log out again).

I'm relatively new to programming with hooks, and I've already tried doing something like this before the <?php get_header( ); ?> call:

<?php if (is_user_logged_in()) { ?>

<?php wp_redirect( 'http://example.com/blog' ); ?> 

<?php } else { ?>

Homepage code in Index.php

<?php } ?>

Any suggestions on either a) where to put this code correctly or b) what function to use to produce the result I am looking for?

Thanks!

19 Answers

Deleted User

Um, thanks? If you read my question, I mentioned this exact plugin as one that I'm already using to redirect users from the login page to the 'blog' page.

What I'm trying to figure out is this: What are some possible Wordpress php solutions to redirecting users automatically to a 'blog' (located at www.example.com/blog) page IF they happen to be logged in AND they enter the url for the homepage (www.example.com). Now that I think about it, Treehouse does almost this exact same thing for logged in users with the /dashboard redirect. So how could this be accomplished?

Nick Pettit
STAFF
Nick Pettit
Treehouse Teacher

Hey Colin,

Great question! I've pinged Zac so that he can respond to you. :)

Deleted User

Thanks!

Sorry Colin, my bad. This is what happens when I try to help people when I'm half asleep :/

Deleted User

Hey, no problem :) Even for a php novice like me, it seems like this problem that I have should be an easy one to fix with all of Wordpress' awesome functionality, but I'm completely stumped. Let me know if you can find a solution — thanks!

I think this should work:

<?php 
    if (is_user_logged_in() ) {
        wp_redirect ( home_url("http://example.com/blog") );
        exit;
    }
?>

Put this on line 1 of your root index.php page.


If the user is NOT logged in then it will proceed with the rest of the script.

Zac Gordon
STAFF
Zac Gordon
Treehouse Guest Teacher

Hey Colin, just wanted hop in here and see if that last suggestion solved the problem?

Deleted User

@Joseph - Thanks for the code; it looks similar to what I was trying before and I put it on line 1 of my index.php page before the <?php get_header(); ?> request, but still nothing :( I have made sure that I am logged in and I type in the homepage url, but it loads the homepage as if nothing has changed.

@Colin

Would you be willing to post your index.php code?

Maybe there is a better place to put that code.

I would next suggest placing the code after the SESSION/LOGIN files have initialized.

It may not be able to do anything because the function is_user_logged_in() isn't defined yet.

Deleted User

@Joseph

Here is the simplified version of my index.php file:

<?php 
    if (is_user_logged_in() ) {
        wp_redirect ( home_url("http://example.com/blog") );
        exit;
    }
?>

<?php get_header(); ?>

    <div id="hero">

       // Simple HTML Code

    </div><!-- #hero feature -->


    <div id="content">
      <div class="center-container">

        // Simple HTML Code

      </div>
    </div><!-- /#pageWrapper -->


  <?php get_footer(); ?>

Here you can see where I attempted to put that code snippet you gave (very top line), but nothing happened.

Also, what line of code initializes the SESSION/LOGIN files you mentioned?

They are probably initialized in the get_ header() function. Try putting that code just after that function.

Deleted User

Okay, so I tried a few things. Putting the code beneath the get_header() function results in basically the same thing as when it was above: a blank screen (though putting it below the header at least loaded the header before a blank rest of the page).

So, I tried the function WITHOUT the home_url () part

<?php 
     if (is_user_logged_in() ) {
        wp_redirect ( "http://example.com/blog" );
        exit;
    }
?>

above and below the get_header, but still nothing. When I tried to see if the wp_redirect function was even working on its own, I tried this:

 <?php wp_redirect ( "http://example.com/blog" ); exit; ?>

above the header and when I loaded the homepage it looked like it was trying to redirect correctly, but wouldn't finish loading the /blog page. I'm pretty confused at this point and I may just think of simplifying by cutting out the landing page completely.

Don't simplify yet. There is always a way.

Do all of the functions I presented exist in your code?

Check case sense and spacing.

Also,

What index.php page are you trying to place this code into?

Try inserting into your themes\index.php page.

Deleted User

Oh, okay — I was putting the code in my specific theme's index.php (homepage) file, or the one located at themes/my-theme/index.php. I've double-checked my spelling and case on all the functions, so I'm pretty sure it was just a placement problem. I will try this, but I am also curious what these different index.php files are. I noticed ones in the /wp-content and root folders, but I've never noticed these before. . .

They are probably put there so if someone were to try to navigate your folder system they would be greeted with a blank page.

Deleted User

I feel incredibly stupid right now — I knew this was far too simple to be causing me so much trouble! I have been doing all this work on my MAMP localhost and throughout this whole process I had incorrectly memorized my localhost IP address and had been plugging in a useless url the whole time!

Thanks so much for helping me out! Here is the code that finally worked for me:

<?php 
    if (is_user_logged_in() ) {
        wp_redirect ( home_url("/blog") );
        exit;
    }
?>

I put this in my theme's index.php file above the get_header request and it works beautifully. When I log out, the homepage displays and when I'm logged in, it redirects me to the /blog page. So everything works great—thanks again!

See. Never give up! Glad you got it going!