admin管理员组

文章数量:1387294

I trying to add a custom field to WP Multisite registration. I found hooks to display my field but I can’t save its value to user profile, because to do that I need user ID, but user ID, as I assumed does’t exist before user will confirm registration by link in email. I tryed this code:

add_action( 'wpmu_new_user', 'phone_register_extra_fields' );
function phone_register_extra_fields ( $user_id, $password = "", $meta = array() ) {
    $phone_number = filter_var($_POST['phone_number'], FILTER_SANITIZE_NUMBER_INT);
    update_user_meta( $user_id, 'phone_number', $phone_number );
}

How can I save the field value to pending user profile that doesn’t have the ID yet? The function update_user_meta() above, doesn't work, because it need user ID.

I fount out, that it is possible to reach user ID on this hook (after activation blog and user in thru email link):

add_action( 'wpmu_activate_blog', 'mu_add_phone_after_activation', 10, 3 );
function mu_add_phone_after_activation( $blog_id, $user_id, $password, $signup_title, $meta ) {
    $user_phone = 'TESTnumber';

    // $user_phone = $meta['user_phone']; // How to put data in the $meta['user_phone']? On which hook to take it from a $_POST['user_phone'] ?

    update_user_meta( $user_id, 'phone_number', $user_phone );      
}

But I need to store the phone number from Multisite User and Blog registration page form. Probably it is possible to store it in the $meta variable, but I tried a lot of hooks and didn’t succeed. May be did it wrong. These are the ways I tried:

    add_filter( 'signup_user_meta', 'mu_add_phone_in_signup', 10, 4 );
function mu_add_phone_in_signup( $meta, $domain, $path, $title, $user, $user_email, $key ) {

    $meta['phone_number'] = '1111111';
    return $meta;
}

add_filter( 'add_signup_meta', 'mu_add_phone_in_signup2', 10, 4 );
function mu_add_phone_in_signup2( $meta_defaults = array() ) {

    $meta_defaults = array( 'phone_number'  => '11111111' );
    return $meta_defaults;
}

add_filter( 'signup_site_meta', 'mu_add_phone_in_signup3', 10, 4 );
function mu_add_phone_in_signup3( $domain, $path, $title, $user, $user_email, $key, $meta ) {

    $meta['phone_number'] = '11111111';
    return $meta;
}

I don't have very experience, so I am going crazy, can't do that 3 days. Will appreciate any halp :) Thank you!

I trying to add a custom field to WP Multisite registration. I found hooks to display my field but I can’t save its value to user profile, because to do that I need user ID, but user ID, as I assumed does’t exist before user will confirm registration by link in email. I tryed this code:

add_action( 'wpmu_new_user', 'phone_register_extra_fields' );
function phone_register_extra_fields ( $user_id, $password = "", $meta = array() ) {
    $phone_number = filter_var($_POST['phone_number'], FILTER_SANITIZE_NUMBER_INT);
    update_user_meta( $user_id, 'phone_number', $phone_number );
}

How can I save the field value to pending user profile that doesn’t have the ID yet? The function update_user_meta() above, doesn't work, because it need user ID.

I fount out, that it is possible to reach user ID on this hook (after activation blog and user in thru email link):

add_action( 'wpmu_activate_blog', 'mu_add_phone_after_activation', 10, 3 );
function mu_add_phone_after_activation( $blog_id, $user_id, $password, $signup_title, $meta ) {
    $user_phone = 'TESTnumber';

    // $user_phone = $meta['user_phone']; // How to put data in the $meta['user_phone']? On which hook to take it from a $_POST['user_phone'] ?

    update_user_meta( $user_id, 'phone_number', $user_phone );      
}

But I need to store the phone number from Multisite User and Blog registration page form. Probably it is possible to store it in the $meta variable, but I tried a lot of hooks and didn’t succeed. May be did it wrong. These are the ways I tried:

    add_filter( 'signup_user_meta', 'mu_add_phone_in_signup', 10, 4 );
function mu_add_phone_in_signup( $meta, $domain, $path, $title, $user, $user_email, $key ) {

    $meta['phone_number'] = '1111111';
    return $meta;
}

add_filter( 'add_signup_meta', 'mu_add_phone_in_signup2', 10, 4 );
function mu_add_phone_in_signup2( $meta_defaults = array() ) {

    $meta_defaults = array( 'phone_number'  => '11111111' );
    return $meta_defaults;
}

add_filter( 'signup_site_meta', 'mu_add_phone_in_signup3', 10, 4 );
function mu_add_phone_in_signup3( $domain, $path, $title, $user, $user_email, $key, $meta ) {

    $meta['phone_number'] = '11111111';
    return $meta;
}

I don't have very experience, so I am going crazy, can't do that 3 days. Will appreciate any halp :) Thank you!

Share Improve this question asked Apr 24, 2020 at 3:26 AndrewAndrew 1214 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 1

I finely made it work this way:

// Sending custom field data to $meta
add_filter('add_signup_meta', 'sb_add_signup_meta');
function sb_add_signup_meta($meta) {
    if( isset( $_POST['phone_number'] ) ) {
        $phone_number = filter_var( $_POST['phone_number'], FILTER_SANITIZE_NUMBER_INT );
        $meta['phone_number'] = $phone_number;
        return $meta;
    }
    else return $meta;
}

// Updating user meta on user activation. Works only if user chose registration without a blog
add_action('wpmu_activate_user','activate_user',10,3);
function activate_user( $user_id, $password, $meta )   {

    if ( isset( $meta['phone_number'] ) ) {
        update_user_meta( $user_id, "phone_number", $meta['phone_number'] );    
    }
}

// Updating user meta on user activation when user has regisration + gets a blog.
add_action( 'wpmu_activate_blog', 'mu_add_roles_after_activation', 10, 3 );
function mu_add_roles_after_activation( $blog_id, $user_id, $password, $signup_title, $meta ) {

    $user_info = get_userdata( $user_id );

    $user_login = $user_info->user_login;

    global $wpdb;

    $signup = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $wpdb->signups WHERE user_login = %s", $user_login ) );

    $meta = maybe_unserialize( $signup->meta );

    if ( isset( $meta['phone_number'] ) ) {
        update_user_meta( $user_id, "phone_number", $meta['phone_number'] );    
    }   
}

Probably WP developers should made hook wpmu_activate_user work when user+blog are activated, because user is beeng still activated here and is confusing, that wpmu_activate_user hook doesn't work on user-with-blog activation. And also, it is not the best practice, to connect to DB every time. So, if you have better ideas, will appreciate it.

本文标签: customizationAdd custom user profile field to default WordPress MultiSite registration form