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.
- According to me, the above code will updates the database in every page load regardless of login.
- What will happen if two users are visiting the site simultaneously where one is logged in and other is not.
- 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.