if / elseif Logic Error
-
Hi, I was hoping someone could point out where on earth my logic is falling apart in this nested if statement.
This is a form of security for my site, basically it’s limiting what each user can see within their roles, the actual implementation is a little complicated without explaining the site in great detail but I can say that I’ve thoroughly checked and the variables work in the correct locations.
Here’s where I’m at:
add_action('pre_get_posts', 'view_my_records'); function view_my_records($q) { if(is_user_logged_in()){ if(in_array('patient', $types)){ if($role == 'patient'){ if($path != $user_check){ wp_redirect(home_url() . $user_check); exit; } elseif($role == 'care_home_admin'){ var_dump("Role is correct"); if($belongs != $logged_in_post_id or $belongs = NULL){ wp_redirect(home_url() . "/home/" . $home_name); exit; } } } } } }I’ve left out the bit where the variables are established as it seemed a waste of time, if it’s required I can post it later.
Here’s how it’s supposed to flow:
If the view is a patient view (the array returns the post_type, which is the view or home in this example):
If the logged in user has the role of patient:
If the end of the url (which would look like siteurl/patient/patient_username)does not match the logged in username:
Redirect user to their own record (so the url ending in their username).
Up until here, logged in as a patient, this works perfectly.
Elseif logged in user has the role of care_home_admin:
If the record on screen does not belong to the users home (this is complicated but basically the database record for each patient has a field showing which home they belong to, this is numerical and would basically be two strings reading “429” != “430” or something like that. If the patient the user is trying to view belongs to them then it should be to same number in each string.
Redirect to their home page for their home.
This second bit doesn’t work even though it’s identical in structure. If I use it as the primary if it causes an infinite redirect loop on every page accept the patients belonging to the home (so it’s obviously close) but as shown above it doesn’t seem to even get called.
I don’t know where I’m going wrong, if this is a numerical string issue or what, so if someone could offer some thoughts it would be greatly appreciated.
Many thanks.
The topic ‘if / elseif Logic Error’ is closed to new replies.