0

In wordpress i want a different homepage for logged-in users and those who are logged-out (simple changing the redirect url when somebody clicks on the title of my website). Is there a way of doing this by adding a code snippet in my themes functions.php? I tried:

add_filter('get_the_permalink','my_permalink_redirect');
function my_permalink_redirect($permalink) {
if( is_user_logged_in() ) { 
global $post;
    if ($post->ID == 1) {
        $permalink = 'http://localhost/homepage-for-logged-users';
    }
    }
    return $permalink;
}

unfortunately it doesn't work and it would be unhandy to change the function from localhost to my domain name when i upload the site to a live hosting. can somebody give me some advice? Thanks!

edit:I got it working with the pluging "login with ajax" where I could define redirects after logins and I putted this snippet in my functions.php.

// Redirect users who arent logged in and on a 404 to the home page for logged out users
function login_redirect() {

    // Check to see if user in not logged in and on the home page for logged users
    if(!is_user_logged_in() && is_404()) {
          // If user is, Redirect to home page for logged out users.
           wp_redirect(home_url('home-logged-out'));
           exit;
}

}
   // add the block of code above to the WordPress template
add_action( 'wp', 'login_redirect' );

*I made the pages for logged-in users private so logged-out users will see 404. Not a very clean method, but it works for now...

Florian
  • 725
  • 6
  • 27

1 Answers1

0

Set your default home page to the page logged in users should see.

Add this code to functions.php:

add_action('init', 'redirect_user');
// for users not logged in
function redirect_user(){
    if( !is_user_logged_in() ) {
        wp_redirect( 'http://www.YourSite.com/SomePage'); 
        exit;
    }
}

Logged in users will see what they should see, and those not logged in will be redirected.

Len_D
  • 1,422
  • 1
  • 12
  • 21
  • Thanks for you answer Len! I tested your code and it gives me a redirect loop error in my browser for logged out users. any ideas? – Florian Jun 13 '14 at 10:39
  • I'm sorry. You are absolutely right. I didn't test this. Very unprofessional of me. I have a meeting now, so let me leave you with this. Modify the !is_user_logged_in() and add an && statement to check that the current page is not the page you are redirecting to. If you are not using a static home page on your site, the global variable $pagename is available. Otherwise you have to take another route (http://stackoverflow.com/questions/4837006/how-to-get-the-current-page-name-in-wordpress). If someone doesn't come along before my appointment ends, I will fix this for you and repost. – Len_D Jun 13 '14 at 12:07
  • Looks like you got this working. Again, I am sorry for the initial confusion. Which approach did you end up taking? – Len_D Jun 13 '14 at 14:19
  • no problem Len_D and thanks for you support. See the edit for my primitive solution. – Florian Jun 16 '14 at 14:15