Redirect users to edit profile if they have empty required fields

This function will redirect users after login to edit profile page and keep redirecting them untill they fill in all the empty required fields.

Make sure to:

  • change link to edit profile page in $redirect_url variable

template_redirect is located in wp-includes/template-loader.php

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
/*
 * Redirect users to edit profile if they have a empty required fields
 */
 
add_action( 'template_redirect', 'wppbc_redirect_incomplete_profiles' );
function wppbc_redirect_incomplete_profiles(){
	$user = get_current_user_id();
	$current_url = wppb_curpageurl();
	$redirect_url = "http://localhost/pb20/edit-profile/";
 
	if ( !empty( $user ) && !current_user_can('manage_options') && ( $current_url != $redirect_url ) ) {
		$fields = get_option('wppb_manage_fields', array());
		foreach ( $fields as $field ){
			if ( $field['required'] == 'Yes' && !empty( $field['meta-name'] ) ){
				$meta_val = get_user_meta( $user, $field['meta-name'] );
				if ( empty( $meta_val ) ){
					wp_redirect( $redirect_url );
					exit();
				}
			}
		}
	}
}