Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I want to add a custom post type (called pupil) into Wordpress when inserting a new user. The wp_insert_user() is used for creating new users.

I have this code:

function register_user_pupil($user_id) {

    //Count nr of occurences in user-table for submitted email-adress
    $email_form = $_POST['email'];
    $sql = "SELECT * FROM $wpdb->users";
    $sql .= " WHERE user_email = '%s'";    
    $count_users_email = intval($wpdb->get_var($wpdb->prepare( $sql, $email)));

    //Check if email (from exists) already exists in the system (in users table)
    //If it doesn't, create new user into variable. If it does, store that user in variable
    //based on email-adress
    if ($count_users_email === 0) {
        $current_user = new WP_User($user_id);
    }
    else {
        $current_user = get_user_by( 'email', $email_form );
    }

    //Create a new pupil custom post-type for this user
    $user_registration_post = array(
      'post_title'      => $current_user->display_name,
      'post_type'       => 'pupil',
      'post_status'     => 'draft',             //This registration cpt is not available for public
      'post_author'     => $current_user->ID
    );
    $postid_pupil = wp_insert_post( $user_registration_post );

    //postid should return something above 0 and not a WP_Error-object
    if (is_array($postid_pupil) || intval($postid_pupil) === 0) {                        
        throw new Exception('Kan inte skapa en elev-registrering');
    }

    //Rest of updating is done by function postsubmit() (saving the pupil CPT)

}
//Hook for handling new users 
add_action( 'user_register', 'register_user_pupil', 10, 3 );

The issue is that this function is called AFTER the user is inserted, I want it to be executed BEFORE the actual user is inserted. I've also tried to use the register_post hook, but this code is not called before the user is inserted anyway.I've also tried to figure out if I could use hooks with pre_{something}, but with no luck.

Anyone could me help me? That would be much appreciated :-)

share|improve this question
1  
You want to insert the post for every failed registration attempt and without knowing the user ID? Why? –  toscho Feb 8 at 14:44
    
I want to do the checking of the users email before, but the ideal would be insert/create the cpt after the user is succesfully created. If the email already exist, I don't want to create a user, but I want to create a CPT for the user with that email. –  bestprogrammerintheworld Feb 8 at 14:49
    
Have you already seen email_exists()? –  kaiser Feb 8 at 15:05

2 Answers 2

up vote 1 down vote accepted

I think you are making this a lot harder than it has to be.

  1. Run your code on user_register and update_profile
  2. Check to see if the post exists, and create it if not
  3. Done. WordPress should take care of the rest.

Something like:

function register_user_pupil($user_id) {
  $current_user = new WP_User($user_id);

  $post_args = array(
    'post_title'      => $current_user->data->display_name,
    'post_type'       => 'pupil',
    'post_status'     => 'draft',             //This registration cpt is not available for public
    'post_author'     => $current_user->ID
  );
  $exists = new WP_Query($args);

  if (!$exists->have_posts()) {
    $postid_pupil = wp_insert_post( $post_args );
  }  

  //postid should return something above 0 and not a WP_Error-object
  if (is_array($postid_pupil) || intval($postid_pupil) === 0) {                        
      throw new Exception('Kan inte skapa en elev-registrering');
  }

  //Rest of updating is done by function postsubmit() (saving the pupil CPT)

}
add_action( 'profile_update', 'register_user_pupil');
add_action( 'user_register', 'register_user_pupil');

I don't have your CPT and supporting code so I can't really test that but I am pretty sure it is close.

WordPress will enforce unique email addresses so I don't see the point of worrying about that.

share|improve this answer
    
Thanks for pointing me in the right direction! :-) I think I will figure it out now! :-) –  bestprogrammerintheworld Feb 9 at 11:31

Try with changing the priority of action, 10 to something like 50

add_action( 'user_register', 'register_user_pupil', 50, 1 );

And you've write 3 as number of arguments pass to your function register_user_pupil but actually you are using just $user_id function register_user_pupil($user_id)

  1. Increased the priority like, 50
  2. Set number of arguments to 1
share|improve this answer
1  
How does this answer the question? Please explain why this might fix the problem. –  s_ha_dum Feb 8 at 16:33

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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