3

I am making a custom (quick) registration page in my wordpress site, to let users to register quickly by just filling username-email and choose their password.

I have tpl-edit-profile.php,

what i need to achieve is to redirect users after they register to that page (tpl-edit-profile.php)

No activation should be needed for registration.

Then users must fill in and complete all profile data, before they can be redirected to the rest of site. In other terms if they don't complete profile information they can't continue to use the site so all action stick to the tpl-edit-profile.php. When they complete profile information they can access and use the whole site.

Thank you.

1
  • @fridtids Please try to use your best English and re-check your question before posting. Also try to make use of the mark up language or the WYSIWG-Editor buttons.
    – kaiser
    Apr 19, 2012 at 2:30

1 Answer 1

5

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();


           }

      }

 }

}
8
  • +1 @ericissocial thank you for taking from your time to improve your answer.please give me some time to try it and tweak it to fit my code, and then come back to you for a feedback. Ps : i am not using gravity form cause it's paid plugin. i am using jobroller theme & i have provided files in a previous post link you can have a look if you want.
    – fritids
    Apr 19, 2012 at 15:27
  • No problem, this was something I had to take the time to learn for a project we are working on right now, and there wasn't really a whole lot of help to refer to out there, so I'm glad to share the knowledge.
    – Eric Allen
    Apr 20, 2012 at 6:23
  • hi.. i can say that your code is very clear and simple to unsderstand. I really liked the pre_get_posts to restrict users from accessing site if they don't get ride of step 2.well done!! unfortunately, add_action('pre_get_posts','check_reg_on_page');' causes an infinite redirect loop Erreur 310 (net::ERR_TOO_MANY_REDIRECTS) : Too much redirections on google chrome. i don't know if it's a conflict with the jobroller theme or with the hook itself. can you help with this ??? NB: my site is on localhost with the virtual host manager of easyphp so i can have maydomain.com [...]
    – fritids
    Apr 20, 2012 at 23:31
  • [..] when i log in site is redirected to http: //mydomain.com//registration-step-2/ as add_action('wp_login','check_reg'); did the job. when i tr to access another part of the site using nav menu my site is supposed to prevent access and stay at http: //mydomain.com//registration-step-2/ and that what i really got in my url. te only matter is that at thes step my ste is loading slowly and at end i got Erreur 310 (net::ERR_TOO_MANY_REDIRECTS) and per url http: //mydomain.com//registration-step-2/. that let me suppose that your code did the trick i want, but i couldn't get rideof error
    – fritids
    Apr 20, 2012 at 23:51
  • 1
    Okay, have you tried rebuilding your permalinks from the Settings? Also, I know this sounds crazy, but have you tried setting your site to not redirect to http://mydomain.com/ but to http://www.mydomain.com/(I can't tell which one it actually goes to because I see typing out just the url shortens it to mydomain.com? Is it possible for you to upload it and test it live somewhere? I haven't tested this script locally, but I'm using almost exactly the same thing on a live server and it works. I've had issues with local testing and some functionality. Do you have any plugins that redirect?
    – Eric Allen
    Apr 22, 2012 at 3:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.