0

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 !

0

To 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:

  1. Filters the wp_query args if a user is logged in, you're on the homepage and it's the main query
  2. 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'
  3. Similarily, if the user has a role employer, use the ID for that page.

Thanks for helping clarify all your points.

  • finale it dosnt work ... – ouanounou Jul 19 '17 at 16:42
  • the homepage not change – ouanounou Jul 20 '17 at 12:40
  • Can you elaborate? I'd like to help, but you need to give more information on what isn't working since you had previously reported that this solution was working. What is not working about it?? – danbrellis Jul 20 '17 at 13:33
  • in add the function like before and I thinking the homepage change but no I the homepage is not change i have delete [ ] because I see in the different discussion they dont have [ ] for id but I don't why Is not working I try to check if the role existing and yes is same to function maybe is my function is not put in good place ? if you want to chat directly you can join chat here : chat.stackexchange.com/rooms/62457/… – ouanounou Jul 20 '17 at 13:37
  • yes, that' right, the square brackets were just to indicate placeholders. It should be $query->set('p', 423); if 423 is the ID of the page – danbrellis Jul 20 '17 at 16:48
1

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
  • if you need to access to my website www.myk-consulting.com – ouanounou Jul 19 '17 at 12:28
  • thanks to reply me , i this the second option is more adapt for my situation I need 3 template if user is not login, if candidate is login and if user company is login so how can be do that I not really understanding the doc and I want to the url of my home page stay www.myk-consulting.com and is not 3 url different is it possible ? thanks – ouanounou Jul 19 '17 at 12:46
  • Yes, it's possible. I misspoke about the hook you should. You'll actually want the template_include filter. I'm putting together a more comprehensive answer- stand by. – danbrellis Jul 19 '17 at 13:02
  • but I dont have homepage-company.php and homepage-candidate.php I have juste 3 different page but I want the url is same – ouanounou Jul 19 '17 at 13:18
  • what do you mean, "3 different page"? The homepage-company.php and homepage-candidate.php are just files in your theme's folder that are the template files. They can be named whatever you want and those files will be displayed instead of home.php (or frontpage.php or index.php). – danbrellis Jul 19 '17 at 13:24
0

i have create my page directly in wordpress like this enter image description here

so I have now just one home page in my file template call index.php

and my 2 page is not in this file

  • By the way, you should edit your original question to add more details, not post an answer. – danbrellis Jul 19 '17 at 13:32
  • Okay, now I see what you mean. I thought you were editting your theme's files. A new answer coming up. – danbrellis Jul 19 '17 at 13:33
  • actually, before I answer, are you able to edit/add any new files in your theme? What theme are you using? Is it a custom theme, child theme, etc? – danbrellis Jul 19 '17 at 13:35
  • yes I can add file in my theme I use job career template I buy this template and I have custom some things is not a child template I dont have activate now because I have some bug with him but is not really hard to fix that i have create all my page directly in wordpress without create file php directly in my template file – ouanounou Jul 19 '17 at 13:39
  • if you need admin access or ftp access I can give you if you want – ouanounou Jul 19 '17 at 13:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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