• Resolved bestprogrammerintheworld

    (@bestprogrammerintheworld)


    Hi!

    I want to create a new custom post type (pupil) related to a new created user. If the user already exists I want to create a new custom post type pupil that is related to the user that has that email.

    I wonder how I could achieve this (in best possible way)?
    (I can’t user user_register-hook because I want to do this BEFORE the user is created. I can’t use register_post because I guess the profile-builder is handling create_new_user() ?)

    Here’s some code so you understand what I’m trying to achieve:

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

    https://www.ads-software.com/plugins/profile-builder/

Viewing 5 replies - 1 through 5 (of 5 total)
  • Thread Starter bestprogrammerintheworld

    (@bestprogrammerintheworld)

    Forgive me for leaving this out: It’s about the front-end registration-part!

    Plugin Contributor barinagabriel

    (@barinagabriel)

    Hello,

    we are using the wp_insert_user WP function to add a new user, and it’s source code can be found here: https://core.trac.www.ads-software.com/browser/tags/3.8.1/src/wp-includes/user.php#L0 – on line 1298.

    In that function, you have a lot of filters you can hook onto to create your CPT (the ones indexed with the “pre_” word).

    Let me know if you need any further info/help regarding this!

    Regards,
    Gabriel

    Thread Starter bestprogrammerintheworld

    (@bestprogrammerintheworld)

    Ok, thanks! I guess I will manage now, but I will let you know if I dont. Thanks once again! ??

    Thread Starter bestprogrammerintheworld

    (@bestprogrammerintheworld)

    I can’t really figure out which hook I should use for this? wp_insert_user() only seem to use pre_{field} for user-fields and not for the “pre-insert-user”-functionality? Probably I’m missing something here…

    Plugin Contributor barinagabriel

    (@barinagabriel)

    Hello,

    the way I see it you have pre_user_email to start creating your CPT, though at the point this filter is run, it only checks for a valid email (same goes for the login-name a few lines before), but the hooks run a few moments after adding the user are in the following condition:

    if ( $update )
    do_action(‘profile_update’, $user_id, $old_user_data);
    else
    do_action(‘user_register’, $user_id);

    Unfortunately these are the only filters/hooks that you could use without overwriting the whole function.

    Regards,
    Gabriel

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘hook before creating new user?’ is closed to new replies.