1

I am trying to get the user ID during registration and automatically add that ID to my custom table. I am using the WP-Members plugin for registration.

Is it possible to get user ID on the fly while registering and add that ID to another custom table with WP-Members plugin.

Or I could use a custom registration page if required; but I need some guidance how to get user ID on the fly during registration.

Anyone please help me...

1
  • The only plugin that I know of that has such a capacity is the User Registration & User Profile – Profile Builder. They create a separate DB table 'signups', which stores all user submitted fields until the user is confirmed. I believe it is a great way of handling WP user registration, and I am really surprised that no one else is using this method. With user data saved in the custom table you can access and use it as you wish.
    – EUGUK
    Commented Nov 2, 2020 at 17:49

3 Answers 3

5

Please take a look at user_register hook

This is fired when a new user is registered and conveniently passes you the user ID of the new user.

function function_name( $user_id )
{        
    /* do what you want to do with ID here */
}
add_action( 'user_register', 'function_name');
6
  • Thanks a lot for quick response. Actually I have never done this before so little confuse and need great help from you experts. So in between this I can use $wpdb inser command for inserting user ID to custom table? and how can I know that I should use $user_id and $user_name etc? sorry for dumb question Commented Aug 30, 2012 at 11:11
  • codex.wordpress.org/Function_Reference/get_userdata will get you all the user details from the ID
    – Scott
    Commented Aug 30, 2012 at 11:20
  • My table name is wp_customs and column is customs_id and I tired below code but didn't insert user id when they registers. function get_customs_id($user_id){ global $wpdb; $wpdb->insert( 'wp_customs', array( 'customs_id' => 'value1' ), array( '%d' ) ); } add_filter('user_register', 'get_customs_id'); Commented Aug 30, 2012 at 11:27
  • Where did you put this code?
    – Scott
    Commented Aug 30, 2012 at 11:30
  • In theme function.php ..I want to get user id and need to inset into that table when user get register Commented Aug 30, 2012 at 12:23
1

@Brady's answer is correct because WP-Members does use WP's function for inserting a new user, so that action does get called. And if you want flexibility and you are only using the new user's ID for all of it, that's a good approach.

But a secondary approach would be to use WP-Members' wpmem_post_register_data action. That action passes all of the user's registration data an array keyed by the field meta keys and it includes ID.

add_action( 'wpmem_post_register_data', 'my_reg_hook' );
function my_reg_hook( $fields ) {
    /* 
     * ID is available as $fields['ID']
     * Other fields are $fields['the_fields_meta_key']
     *
     * do your stuff here
     */
    return;
}

The documentation for the action has a list of fields that will be included in the array in addition to any custom fields that are in there by meta key:

https://rocketgeek.com/plugins/wp-members/docs/filter-hooks/wpmem_post_register_data/

0
add_action( 'user_register', 'usermeta_update');

function usermeta_update( $user_id )

{ 
  
    add_action('woocommerce_created_customer', 'save_data', $user_id);

}

//save data
function save_data($customer_id)

{

    //First name field
    if (isset($_POST['billing_first_name'])) {
        update_user_meta($customer_id, 'first_name', sanitize_text_field($_POST['billing_first_name']));
        update_user_meta($customer_id, 'billing_first_name', sanitize_text_field($_POST['billing_first_name']));
    }

    //Last name field
    if (isset($_POST['billing_last_name'])) {
        update_user_meta($customer_id, 'last_name', sanitize_text_field($_POST['billing_last_name']));
        update_user_meta($customer_id, 'billing_last_name', sanitize_text_field($_POST['billing_last_name']));
    }
}
2
  • Consider adding explanation about what this code does, where to implement it and how it solves OPs problem. Copy paste code without any information is not very helpful. Also there is already an accepted answer, so you should also explain what your answer does differently, or how it's better. Commented Dec 13, 2021 at 7:13
  • Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.
    – Community Bot
    Commented Dec 13, 2021 at 20:53

Your Answer

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

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