You can add user meta data to a new registered user depending on their role using the user_register hook in WordPress. Here’s an example of how to use this hook to add user meta data to a new user based on their role:

function add_user_meta_on_registration( $user_id ) {
    // Get the user object
    $user = get_user_by( 'ID', $user_id );

    // Check if the user has a specific role
    if ( in_array( 'subscriber', (array) $user->roles ) ) {
        // Add user meta data for subscribers
        add_user_meta( $user_id, 'subscription_level', 'basic' );
    } elseif ( in_array( 'contributor', (array) $user->roles ) ) {
        // Add user meta data for contributors
        add_user_meta( $user_id, 'articles_written', 0 );
    }
}
add_action( 'user_register', 'add_user_meta_on_registration' );

In this example, we use the user_register hook to call the add_user_meta_on_registration function when a new user is registered. Inside the function, we get the user object for the new user using the get_user_by function, and then check the user’s role using the in_array function. If the user has a subscriber role, we add a subscription_level user meta key with the value of basic. If the user has a contributor role, we add an articles_written user meta key with the value of 0.

Note that you may need to adjust the code above depending on your specific use case. This example assumes that you want to add user meta data based on the user’s role, but you can modify the code to add user meta data based on other criteria if needed.

(Visited 42 times, 1 visits today)
Was this article helpful?
YesNo
Close Search Window