• Resolved udelledo

    (@udelledo)


    Hi,

    I’m trying to tweak my wordpress installation to perform a validation for logged in user and redirect them to the proper page in case.

    My main purpose is to guarantee that different roles can have a different minimum of required profile fields (ie. I might need to know the phone number of my editors while I don’t need to know the phone number of my subscribers)

    After looking around docs, tutorials and forum I came out with the following solution:

    function validate_user_profile(){
    	//Check user membership
    	if(is_user_logged_in() && pmpro_hasMembershipLevel(2) && !is_page( 'Account' )){
    		//Required fields
    		$field_to_check = array('codice_fiscale','citta_nascita','data_nascita','citta_residenza','telefono','cap','via');
    
    		$user_ID = get_current_user_id();
    		for($i = 0; $i < count($field_to_check); ++$i) {
    			$field_value = get_user_meta($user_ID, $field_to_check[$i], 'TRUE'); //Retrieve field value
    			if(empty($field_value))
    				wp_redirect(get_permalink(get_page_by_title('Account')->ID)); //Redirect to profile page if value is missing
    			exit;
    		}
    	}
    }
    add_action('wp_head', 'validate_user_profile');

    The code above added in the active theme functions.php file did the trick but just partially: when I log in with a user that has to be redirected to the account page the page is loaded correctly, but if the redirect is not executed I can see only a blank page.

    My understanding was that if the code is not executed wordpress should work without interferences, but this doesn’t appear to be the case.

    Can you please help me understand what I am missing in my code snippet?

    Should an explicit redirect to the referer be the solution?

    Thanks and regards

Viewing 2 replies - 1 through 2 (of 2 total)
  • Thread Starter udelledo

    (@udelledo)

    After playing around some more with filters and action I came out with the following working solution

    function validate_user_profile($redirect_to){
    	//Check user membership
    	if(is_user_logged_in() && pmpro_hasMembershipLevel(2) && !is_page( 'Account' )){
    		//Required fields
    		$field_to_check = array('codice_fiscale','citta_nascita','data_nascita','citta_residenza','telefono','cap','via');
    
    		$user_ID = get_current_user_id();
    		for($i = 0; $i < count($field_to_check); ++$i) {
    			$field_value = get_user_meta($user_ID, $field_to_check[$i], 'TRUE'); //Retrieve field value
    			if(empty($field_value)){
    				wp_redirect(get_permalink(get_page_by_title('Account')->ID)); //Redirect to profile page if value is missing
    			exit;}
    		}
    	}
    }
    add_action('wp', 'validate_user_profile');

    I’m not sure why using the wp_head filter wasn’t working but gladly now it does ??

    However I just noticed a bug in this code: unless the user does not fill the details he’s not allowed to logout!!!

    I’ll look for a way to check if the page is the logout link to add in the first if condition and that should be it

    Thread Starter udelledo

    (@udelledo)

    It was easier than I thought
    is_page('logout')

    worked like a charm

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘user validation and redirect cause blank page’ is closed to new replies.