ulgm_new_user_registration_by_form_after
Fires after a new user has been successfully registered via a form, providing the new user's ID and post data.
add_action( 'ulgm_new_user_registration_by_form_after', $callback, 10, 2 );
Description
Fires after a new user is successfully registered via a form. Developers can use this hook to perform custom actions with the new user's ID and submitted form data, such as assigning additional meta or triggering custom notifications.
Usage
add_action( 'ulgm_new_user_registration_by_form_after', 'your_function_name', 10, 2 );
Parameters
-
$new_user_id(mixed) - The ID of the newly registered user.
-
$_POST(mixed) - This parameter contains the ID of the newly created user.
Examples
<?php
/**
* Example function to run after a new user is registered via the ULGMP form.
* This example adds the new user to a specific WordPress role if they weren't already assigned one.
*
* @param int $new_user_id The ID of the newly registered user.
* @param array $post_data The $_POST data submitted with the registration form.
*/
function my_ulgm_post_registration_user_role_update( $new_user_id, $post_data ) {
// Ensure we have a valid user ID and that it's not a WP_Error object.
if ( ! is_numeric( $new_user_id ) || is_wp_error( $new_user_id ) ) {
return;
}
// Define the default role to assign if not already set or if a specific condition is met.
$default_role = 'subscriber'; // Or any other desired role.
// Get the user object.
$user = get_user_by( 'id', $new_user_id );
// Check if the user has any roles assigned. If not, assign the default role.
// This could be useful if the form doesn't explicitly set a role or if you want to ensure a base role.
if ( ! $user->roles || empty( $user->roles ) ) {
$user->set_role( $default_role );
}
// Example: Add a user to a specific role if a certain field was checked in the form.
// Assuming there was a checkbox field with name 'opt_in_premium' in the $_POST data.
if ( isset( $post_data['opt_in_premium'] ) && 'yes' === $post_data['opt_in_premium'] ) {
$premium_role = 'premium_member'; // Define your premium role slug.
// Check if the user already has the premium role to avoid duplicates.
if ( ! in_array( $premium_role, $user->roles ) ) {
$user->add_role( $premium_role );
// Optionally, you might want to remove them from a lower role if this is an upgrade.
// For simplicity, this example just adds.
}
}
// You could also:
// - Add custom user meta data.
// - Trigger other specific actions based on form data.
// - Update existing user meta if the user is re-registering or has an existing profile.
}
add_action( 'ulgm_new_user_registration_by_form_after', 'my_ulgm_post_registration_user_role_update', 10, 2 );
Placement
This code should be placed in the functions.php file of your active theme, a custom plugin, or using a code snippets plugin.
Source Code
src/classes/group-management/group-management-registration.php:259
'user_registered' => date( 'Y-m-d H:i:s' ),
'role' => $role,
),
$_POST
)
);
do_action( 'ulgm_new_user_registration_by_form_after', $new_user_id, $_POST );
if ( is_wp_error( $new_user_id ) ) {
$this->ulgm_registration_errors()->add( 'user_error', __( 'Unable to create user. Please contact site admin.', 'uncanny-learndash-groups' ) );
return;
}
// send an email to the admin alerting them of the registration