I am looking for a function to fill a user meta data automatically when they register.
In the registration form we ask for user email. Once they validate their registration, we want to get that value and store it in a custom field (already created) named "Form_URL" like this : /
So we need to concatenate "/" the user email and "/form/" and store it in the custom fied "Form_URL" after the registration.
So we start the following function but we are blocked :
add_action('user_register', 'add_form_link', 10, 1);
function add_form_link($user_id)
{
$emailurl = '/' . ???? . '/form/';
add_user_meta( $user_id, 'Form_URL', $emailurl , true );
}
PS : Our website is a Wordpress and we use Gravity Forms as the registration Form.
I am looking for a function to fill a user meta data automatically when they register.
In the registration form we ask for user email. Once they validate their registration, we want to get that value and store it in a custom field (already created) named "Form_URL" like this : https://www.ourwebsite.com/USEREMAIL/form/
So we need to concatenate "https://www.ourwebsite.com/" the user email and "/form/" and store it in the custom fied "Form_URL" after the registration.
So we start the following function but we are blocked :
add_action('user_register', 'add_form_link', 10, 1);
function add_form_link($user_id)
{
$emailurl = 'https://www.ourwebsite.com/' . ???? . '/form/';
add_user_meta( $user_id, 'Form_URL', $emailurl , true );
}
PS : Our website is a Wordpress and we use Gravity Forms as the registration Form.
If this is being run when you register a user, then you could just grab the email used in creating the user as it is contained in the second argument that is passed to user_register
which is $userdata
. See https://developer.wordpress.org/reference/hooks/user_register/
Note that to provide you with an example that would actually result in something useable, I also added sanitize_title
which is going to make the result "URL safe". A raw email address is not a URL-safe value as the "@" is not a useable value.
add_action('user_register', 'add_form_link', 10, 2);
function add_form_link( $user_id, $userdata ) {
$emailurl = 'https://www.ourwebsite.com/' . sanitize_title( $userdata['user_email'] ) . '/form/';
add_user_meta( $user_id, 'Form_URL', $emailurl );
}
To make this more portable, I would recommend NOT saving the full URL, but rather everything after the domain. Then when using, you can append the local domain by using home_url()
. Otherwise, by putting in a complete URL, you can't make adjustments to the saved value later without manipulating the string result itself.
You can use the wp_get_current_user() function to get the user object for the currently registered user. From there, you can access their email address using $user->user_email. You can then use this email address to build the URL that you want to store in the user meta field.
Here's an updated version of your function that should work:
add_action('user_register', 'add_form_link', 10, 1);
function add_form_link($user_id)
{
// Get the user object for the currently registered user
$user = wp_get_current_user();
// Build the URL using the user's email address
$emailurl = 'https://www.ourwebsite.com/' . $user->user_email . '/form/';
// Add the URL to the user meta field
add_user_meta( $user_id, 'Form_URL', $emailurl , true );
}
You may also want to consider using the update_user_meta() function instead of add_user_meta(), in case the user has already registered and you want to update the value of the user meta field instead of adding a new one.