I work for a manufacturer that has normal boring consumers and wholesale dealers. We want to display a different home page for these two different audiences. Consumers will get more of a brand overview, while dealers will see current promotions available only to them.
Dealers will have to log in to see this, and I have already created a role for them called "Wholesale." I have also created two home pages: one for consumers (which is set in Wordpress to be the front page) and the one for dealers. The dealer page is a copy and paste of the consumer page, which was built with Elementor.
I have come up with a function (in the theme's function.php file) that does exactly what I want, except for one flaw: the layout of the dealer home page content is broken.
Here's the code:
// Display different home page for dealer vs consumer
function change_home_dealer($content) {
if (is_user_logged_in()) {
$current_user = wp_get_current_user();
$user = new WP_User($current_user -> ID);
if (in_array('Wholesale', $user->roles) && is_front_page()){
// Grab ID of the page I want to be the dealer front page
$dealer_front_id = 2885;
$dealer_content = get_post($dealer_front_id);
$content = $dealer_content->post_content;
return $content;
} else {
return $content;
}
} else {
return $content;
}
}
add_filter('the_content', 'change_home_dealer');
I work for a manufacturer that has normal boring consumers and wholesale dealers. We want to display a different home page for these two different audiences. Consumers will get more of a brand overview, while dealers will see current promotions available only to them.
Dealers will have to log in to see this, and I have already created a role for them called "Wholesale." I have also created two home pages: one for consumers (which is set in Wordpress to be the front page) and the one for dealers. The dealer page is a copy and paste of the consumer page, which was built with Elementor.
I have come up with a function (in the theme's function.php file) that does exactly what I want, except for one flaw: the layout of the dealer home page content is broken.
Here's the code:
// Display different home page for dealer vs consumer
function change_home_dealer($content) {
if (is_user_logged_in()) {
$current_user = wp_get_current_user();
$user = new WP_User($current_user -> ID);
if (in_array('Wholesale', $user->roles) && is_front_page()){
// Grab ID of the page I want to be the dealer front page
$dealer_front_id = 2885;
$dealer_content = get_post($dealer_front_id);
$content = $dealer_content->post_content;
return $content;
} else {
return $content;
}
} else {
return $content;
}
}
add_filter('the_content', 'change_home_dealer');
Share
Improve this question
edited Jun 10, 2020 at 16:11
MCDAndrew
asked Jun 10, 2020 at 15:54
MCDAndrewMCDAndrew
12 bronze badges
1 Answer
Reset to default 0I do believe I've got it. Here's the code:
// Display different home page for dealer vs consumer
if( is_user_logged_in() ) {
$current_user = wp_get_current_user();
$user = new WP_User($current_user -> ID);
if (in_array('Wholesale', $user->roles)){
$page = get_post(2885);
update_option( 'page_on_front', $page->ID );
update_option( 'show_on_front', 'page' );
} else {
$page = get_post(2847);
update_option( 'page_on_front', $page->ID );
update_option( 'show_on_front', 'page' );
}
} else {
$page = get_post(2847);
update_option( 'page_on_front', $page->ID );
update_option( 'show_on_front', 'page' );
}
I'd be lying if I said I'm positive I understand it. However, from what I gather my original code was essentially trying to change the page content in the middle of that content actually being spat out.
On the other hand, this code (a spin on this: https://stackoverflow/questions/20039607/different-wordpress-front-page-for-logged-out-and-logged-in-users) actually changes the front page setting before Wordpress grabs it to pull the data.
It's like telling a worker to grab the correct product off the shelf as opposed to trying to shove the right product in their hands after they've grabbed the wrong one.