I have two different user roles on my Website: Employer and Candidate. Every one had a type of profile but the profile from Candidate should see only employers and nobody else.
So I want a restriction in WordPress like:
Employer CAN see Candidate Candidate CAN’T see other Candidate Candidate CAN see own Profile
This is controlled by a plugin but it seems to be broken BECAUSE:
Employer CAN see Candidate Candidate CAN see other Candidate Candidate CAN’T see own Profile
In the .php file from candidate profile is this code:
<?php if (!$show_candidate_public_profile) { if ($candidate->get_public_account() || get_current_user_id() == $candidate->get_author_id()) { $check = 1; } else { $check = 2; } } else { if (is_user_logged_in()) { if ($show_candidate_public_profile == 2 && get_current_user_id() == $candidate->get_author_id()) { if ($user->is_employer() && $candidate->get_public_account()) { $check = 3; } else { $check = 4; } } else { if ($candidate->get_public_account() || get_current_user_id() == $candidate->get_author_id()) { $check = 1; } else { $check = 2; } } } else { $check = 0; } }
and the code for results right after code from above:
if (!$check) { ?> <div class="iwj-alert-box"> <div class="container"> <span> <?php echo sprintf(__('You must be logged in to view this page. <a href="%s">Login here</a>', 'iwjob'), add_query_arg('redirect_to', $candidate->permalink(), $login_page_id)); ?> </span> </div> </div> <?php } else { if ($check == 2) { ?> <div class="iwj-alert-box"> <div class="container"> <span> <?php echo esc_html__('This profile is not public now.', 'iwjob'); ?> </span> </div> </div> <?php } elseif ($check == 4) { ?> <div class="iwj-alert-box"> <div class="container"> <span> <?php echo esc_html__('This profile is not public or only employers can see.', 'iwjob'); ?> </span> </div> </div> <?php } else { ?> <div class="iw-parallax" data-iw-paraspeed="0.1" style="background-image: url('<?php echo esc_url($cover_image_url); ?>');"></div> <div class="iw-parallax-overlay"></div> <div class="content-top"> <div class="container"> <div class="info-top"> <div class="candidate-logo">
The check 3 is the only one check showing profile.
I tried to change the “&&” “||” “==” but i can’t figure it out how this logic work.
So much php is too much for me. I have asked the Plugin creator but I am always waiting 5 days to reply and I need it now.
I would be very happy if someone would help me with this.
Thank you very much!
Martin
Advertisement
Answer
This code should work according to the behavior you have described(of course it will depend on the good functionality of your plugin). I had to take off some conditions since I don’t know what they are and also you didn’t provide further details, if you need them you have to add later, but this is quite simple and this code is much more readable.
First portion:
<?php if(!is_user_logged_in()) $check=false; //if user is not logged in check is false else { //check if user is employer or if is the profile owner if ($user->is_employer() || get_current_user_id() == $candidate->get_author_id()) $check = 1; //sets 1 if allowed else $check = 2; //sets 2 if denied } ?>
Second portion:
if (!$check) //is check false? then show login message { ?> <div class="iwj-alert-box"> <div class="container"> <span> <?php echo sprintf(__('You must be logged in to view this page. <a href="%s">Login here</a>', 'iwjob'), add_query_arg('redirect_to', $candidate->permalink(), $login_page_id)); ?> </span> </div> </div> <?php } else //check it's not false, so do more tests { if ($check == 2) //if equals 2 then shows access denied message { ?> <div class="iwj-alert-box"> <div class="container"> <span> <?php echo esc_html__('This profile is not public now or only employers can see.', 'iwjob'); ?> </span> </div> </div> <?php } elseif($check == 1) //user is profile owner or is an employer, show everything { ?> <div class="iw-parallax" data-iw-paraspeed="0.1" style="background-image: url('<?php echo esc_url($cover_image_url); ?>');"></div> <div class="iw-parallax-overlay"></div> <div class="content-top"> <div class="container"> <div class="info-top"> <div class="candidate-logo">
If you want more tests, like about profile being public or not, you really have to provide more information. I hope it can help you.