Force users to complete their profile after they register? How to

You should hook into the user_register action.

I did something similar on a recent site (not involving having to fill out profile fields, but involving having to renew a membership).

I will propose a multi-part solution (let me know if any of it is confusing, it’s been a long day and I might not explain it perfectly).

EDIT: Upon thinking about it further, I’d recommend you turn that PHP file into a template and set it as a page, as well. It will make it easier to have WordPress redirect users there. (for the sake of the code below, let’s say you have it set to /registration-step-2/)

First, add an action on the user_register hook where you redirect using wp_redirect([insert url of profile editing page here]).

Something like:

//run this when a new user registers
add_action('user_register','continue_to_registration');
//redirects user to registration form
function continue_to_registration() {
     wp_redirect(get_option('siteurl') . '/registration-step-2/');
     exit();
}

Second, when a user finishes that registration set a custom user_meta option or add a user capability, my personal preference here would be a capability, but you could go either way. This will allow you to check on log-in if the user has finished registering or not.

For the sake of the code below, let’s pretend you have a variable for your form submission called $verified and it is set to true once the form is successfully processed.

You’ll need to add some extra code to the processing part of your form. Something like:

//set user capability on finished registration
if($verified) {
     $user_id = get_current_user_id();
     $user = new WP_User($user_id);
     $user->add_cap('finished_registration');
}

Third, you’ll need to hook into two actions: wp_login (to check if the user logging in has your custom meta option or capability) and you’ll probably also want to add a similar check to pre_get_posts (so that if the user hasn’t finished the registration they will be prompted to whenever they try to navigate anywhere).

Something like:

//run this function when a user logs in
add_action('wp_login','check_reg');
//check if user has finished registration when logging in
function check_reg($login) {
     $user = get_userdatabylogin($login);
     if(!$user->has_cap('finished_registration')) {
          wp_redirect(get_option('siteurl') . '/registration-step-2/');
          exit();
     }
}

//run this function when a user tries to load a page
add_action('pre_get_posts','check_reg_on_page');
//check if user has finished registration before loading a page
function check_reg_on_page() {
     if(is_user_logged_in()) {
          if(!is_page('Registration Step 2') && !is_admin()) {
               $user_id = get_current_user_id();
               $user = new WP_User($user_id);
               if(!$user->has_cap('finished_registration')) {
                    wp_redirect(get_option('siteurl') . '/registration-step-2/');
                    exit();
               }
          }
     }
}

Make sure on the pre_get_posts action that you check for is_user_logged_in() and also that they have your custom meta option or capability.

I can try to provide some code tomorrow when I’m at the office, but if you’re fairly familiar with WordPress actions, you should be okay.

Also, as a side note, I’d make this a simple functionality plug-in so you don’t have to worry about remembering to copy over functions.php stuff if you switch themes down the road.

But, to ensure that they actually fill it out, I would also set a user capability or a custom user_meta option after they have filled it out so that you can tell the system they have finished.

EDIT: IF you’re using GravityForms, let me know, because you may want to hook into a different action due to the order in which they fire. Gravity Forms action gform_user_registered fires after the user_register function, and includes all the data from the particular form you attach it to, which allows you greater flexibility in processing the form and altering users based on the form.

EDIT 2: I’ve added code to better articulate what I was saying. Let me know if you need any clarification or explanation. It will obviously require some tweaking to fit into your code, but the functions I’ve included should serve you well if you just adjust the page name on the redirect.

UPDATE BY FRITIDS :

//run this function when a user tries to load a page
 add_action('appthemes_before_header','check_reg_on_page'); // appthemes_before_header     built-in action hook for jobroller theme
//add_action('pre_get_posts','check_reg_on_page'); // causes redirections loop  error  with jobroller theme
//check if user has finished registration before loading a page
function check_reg_on_page() {
     if(is_user_logged_in()) {
      if(!is_page('Registration Step 2') && !current_user_can( 'administrator' ))       {//!is_admin() is not workin replaced by !current_user_can
           $user_id = get_current_user_id();
           $user = new WP_User($user_id);
            $redirect = get_option('siteurl') . '/registration-step-2/';
           if(!$user->has_cap('finished_registration')) {
                wp_safe_redirect($redirect);

              exit();


           }

      }

 }

}

Leave a Comment