1

After googling, I have a piece of code to set a specific page as home page in wordpress. The code says,

add_action('init', 'loginCheck');

function loginCheck() {

    if (is_user_logged_in()) {
        $page = get_page_by_title('My Account');
    } else {
        $page = get_page_by_title('Shop');
    }

      update_option('page_on_front', $page->ID);
      update_option('show_on_front', 'page');
}

But myself have few considerations regarding above code.

  1. According to me, the above code will updates the database in every page load regardless of login.
  2. What will happen if two users are visiting the site simultaneously where one is logged in and other is not.
  3. Is there any better ways to switch the home page based on login? ie, for logged in user can access different pages through the menu with a custom homepage whereas guest user should only access a specific page ideally a different genaralized public home page and if he/she tries to access other pages should redirect back to general home page only.

1 Answer 1

3

About your considerations:-

  • Yes true! It will update the page on every visit.
  • The database will be updated more frequently based on which user making a request. But there will be no incorrect output.
  • The better way I suggest using a template_include filter. Assign a static front page then apply the template to that page by checking if user logged in or not. Thus you can display different layout/design to logged-in users and visitor.

See this example:-

add_filter( 'template_include', 'home_page_template', 99 );
/**
 * Home page Template based on user
 * @param type $template
 * @return type
 */
function home_page_template( $template ) {
    if (!is_front_page()) {
        return $template; //Return if not home page
    }

    $new_template = false;

    if (is_user_logged_in()) {
        $new_template = locate_template( array( 'logged-in-users.php' ) );
    } else {
        $new_template = locate_template( array( 'not-logged-in-users.php' ) );
    }

    if ( !empty($new_template) ) {
        return $new_template ; //Only return if template exist
    }

    return $template; //Always return value from filter
}

For the rest of the things you can create two menus for logged-in and guest users and call them conditionally in header file. To revoke the access to some pages/posts for guest users use init hook with is_user_logged_in() and do temporary redirect to some page. You can search this site for examples of this method.

2
  • Thanks for the answer. I have tried with the code by creating a front page template front-page.php and setting it with home, add_filter( 'template_include', 'home_page_template', 99 ); function home_page_template( $template ) { if (is_user_logged_in()) { return $template; } else { $new_template = locate_template( array( 'non-logged-in.php' ) ); return $new_template ; } } But one issue is if guest user opens some page other than front page, the permalink shows full eventhough the template non-logged-in.php was set to front page. How to resolve it?
    – mpsbhat
    Apr 28, 2016 at 12:38
  • Please check the code I've posted. The first condition //Return if not home page You must return the current $template if it is not front-page because this filter calls every where not just front-page.
    – Sumit
    Apr 28, 2016 at 12:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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