WordPress.org

Forums

[resolved] if / elseif Logic Error (4 posts)

  1. theslink2000
    Member
    Posted 2 years ago #

    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.

  2. bcworkz
    Member
    Posted 2 years ago #

    In this line
    if($belongs != $logged_in_post_id or $belongs = NULL){
    you are assigning NULL to $belongs with '=' instead of comparing with '=='

    Unfortunately, you dropped the closing backtick, so the forum's parser may have mangled your code, so the problem may be elsewhere.

    FWIW, I don't like comparing NULL and similar values, the comparison does not always yield the results you might expect. I would use either is_null() or isset(), depending on the context of how the variable gets assigned values.

  3. theslink2000
    Member
    Posted 2 years ago #

    Hi bud,

    Sorry about the mild confusion I meant to take out the second half of that line, the

    or $belongs = NULL

    bit was simply me experimenting as other pages (ie. not patients) won't have a belongs field.

    I've reposted the code here with the close in place (I hope) any more ideas?

    if(in_array('patient', $types)){
    	if($role == 'patient'){
    		if($path != $user_check){
    			wp_redirect(home_url() . $user_check);
    			exit;
    		}
    		elseif($role == 'care_home_admin'){
    if($belongs != $logged_in_post_id){
    			wp_redirect(home_url() . "/home/" . $home_name);
    			exit;
    					}
    				}
    			}
    		}
    	}
    }
  4. theslink2000
    Member
    Posted 2 years ago #

    I just figured it out for myself, it was as simple as I thought it might be, the elseif was incorrectly nested for the logic to work. I accidently put it as a second condition to the third if, not the second.

    Simply moving the closing } to the correct place resolved everything!

Topic Closed

This topic has been closed to new replies.

About this Topic