I have develop a wordpress website consulting and I have different user role
and I want to have different content according to user role
so I have a landing page with authentification and according the user role login , the homepage is different
so I would like to say if plugin who make something like this exist or if can code for that
so recap :
Home page for not login is landing page home page for user login is different according to user role
Thanks !
I have develop a wordpress website consulting and I have different user role
and I want to have different content according to user role
so I have a landing page with authentification and according the user role login , the homepage is different
so I would like to say if plugin who make something like this exist or if can code for that
so recap :
Home page for not login is landing page home page for user login is different according to user role
Thanks !
Share Improve this question asked Jul 19, 2017 at 11:51 ouanounououanounou 51 silver badge6 bronze badges4 Answers
Reset to default 0To use a different page's content on the homepage based on a logged-in user's role you can do this:
In your functions.php file add this code:
function wpse_273872_pre_get_posts( $query ) {
if ( $query->is_main_query() && is_user_logged_in() ) {
//work-around for using is_front_page() in pre_get_posts
//known bug in WP tracked by https://core.trac.wordpress.org/ticket/21790
$front_page_id = get_option('page_on_front');
$current_page_id = $query->get('page_id');
$is_static_front_page = 'page' == get_option('show_on_front');
if ($is_static_front_page && $front_page_id == $current_page_id) {
$current_user = wp_get_current_user();
$user = new WP_User($current_user->ID);
if (in_array('cs_candidate', $user->roles)) { //assuming the role name is cs_candidate
$query->set('page_id', [page-id-for-candidate-homepage]);
} elseif (in_array('cs_employer', $user->roles)) { //assuming the role name is cs_employer
$query->set('page_id', [page-id-for-employer-homepage]);
}
}
}
}
add_action( 'pre_get_posts', 'wpse_273872_pre_get_posts' );
What this does is:
- Filters the wp_query args if a user is logged in, you're on the homepage and it's the main query
- If the user has the role 'candidate', set the post id (p) to the ID of the page you created and change the post_type to 'page'
- Similarily, if the user has a role employer, use the ID for that page.
Thanks for helping clarify all your points.
Two options come to mind. Which one will depend on how different your content is for logged in/not logged in.
1) The first, you can just use IF statements on the homepage template.
$current_user = wp_get_current_user();
$user = new WP_User( $current_user ->ID);
if($user && in_array('my-role', $user->roles)){
//stuff specific to users with 'my-role'
}
.
.
.
else{
//stuff for non logged in users or ones that don't match any of your roles
}
2) The second option is to filter the template based on their role. You can hook into the template_include
filter. See the examples in the codex: https://codex.wordpress.org/Plugin_API/Filter_Reference/template_include. With this option you could have multiple templates for various users and use a different one dynamically if you keep consistent with a template naming convention.
Hopefully this helps. Depending on which option you take I can help provide some more detailed code examples.
UPDATED
Based on the information in your comments, here is an example using the template_include
filter hook. You can put this in your functions.php. Untested code:
function wpse_273872_template_include($template) {
//if user is not logged in, just return and show the default homepage
if(!is_user_logged_in()) return $template;
$new_template = '';
$current_user = wp_get_current_user();
$user = new WP_User( $current_user->ID);
if(in_array('candidate', $user->roles)){ //assuming the role name is candidate
$new_template = locate_template( array( 'homepage-candidate.php' ) );
}
elseif(in_array('company', $user->roles)){ //assuming the role name is company
$new_template = locate_template( array( 'homepage-company.php' ) );
}
if ( '' != $new_template ) {
$template = $new_template;
}
return $template;
}
add_filter( 'template_include', 'wpse_273872_template_include' );
The homepage-candidate.php and homepage-company.php are template files that reside in your theme's folder. Here is an example of the file structure:
- my-custom-theme
|- style.css
|- functions.php
|- frontpage.php
|- homepage-candidate.php
|- homepage-company.php
i have create my page directly in wordpress like this
so I have now just one home page in my file template call index.php
and my 2 page is not in this file
I use Gravity Forms and with the User Registration, I have a custom field, "UserType2". Depending on the UserType2 variable in the MySQL UserMeta Table I give different access to Fields on my Gravity Form linked to my Homepage with Conditional Logic. Then I can have as many as I want.
This way I don't need to change anything on the PHP files.