Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Different WordPress front page for logged out and logged in users

Tags:

php

wordpress

The WordPress reading settings can be found under Settings > Reading.

I have the WordPress 'Front page displays' option set to 'Static front page'.

My 'Front page' is set to 'About'.

I am trying to have a different front page for logged out and logged in users. Currently everyone views the 'About' page when they visit example.com.

  • If I visit example.com when logged out I want to see my 'about page'
  • If I visit example.com when logged in I want to see my 'contact page'

Does anybody know how I can achieve this?

like image 385
henrywright Avatar asked Nov 18 '13 03:11

henrywright


2 Answers

It's possible to set the front page programmatically but not sure if this is a solution to your question, so, paste this code in your functions.php file and give it a try

if( is_user_logged_in() ) {
    $page = get_page_by_title( 'Contact Me');
    update_option( 'page_on_front', $page->ID );
    update_option( 'show_on_front', 'page' );
}
else {
    $page = get_page_by_title( 'About Me' );
    update_option( 'page_on_front', $page->ID );
    update_option( 'show_on_front', 'page' );
}

P/S: Not tested, just try it and please response what you get.

like image 125
The Alpha Avatar answered Sep 30 '22 15:09

The Alpha


For anyone else that has this problem you could just add this to the header.php, at the very top before the <!DOCTYPE html>:

<?php if(is_front_page()) {
    if (is_user_logged_in()) {
        $newURL = 'http://YourWebsiteURLhere.com';
        header('Location: '.$newURL);
    }
} ?>

OR you can add this to Functions:

function homepage_template_redirect()
{
    if( is_front_page() && is_user_logged_in() )
    {
        wp_redirect(get_page_link('contact'));
        exit();
    }
}
add_action( 'template_redirect', 'homepage_template_redirect' );
like image 35
JCBiggar Avatar answered Sep 30 '22 16:09

JCBiggar