ulgm_code_group_created
Fires after a new code group has been successfully created, providing the ID and attributes of the new group.
add_action( 'ulgm_code_group_created', $callback, 10, 2 );
Description
Fires after a new code group is successfully created in the database. Developers can use this hook to perform actions related to the newly created group, such as associating it with additional data or triggering notifications. The hook provides the `$code_group_id` and `$attr` for the created group.
Usage
add_action( 'ulgm_code_group_created', 'your_function_name', 10, 2 );
Parameters
-
$code_group_id(mixed) - This parameter contains the ID of the newly created code group.
-
$attr(mixed) - This parameter contains the unique identifier for the newly created code group.
Examples
// This function will be triggered after a new code group is created.
// It demonstrates how to access the newly created group's ID and its attributes.
// For example, you might want to send a notification email to the user or log this event.
add_action( 'ulgm_code_group_created', 'my_custom_code_group_creation_handler', 10, 2 );
/**
* Handles the creation of a new code group by performing some custom actions.
*
* @param int|string $code_group_id The ID of the newly created code group.
* @param array $attr The attributes used to create the code group.
*/
function my_custom_code_group_creation_handler( $code_group_id, $attr ) {
// Ensure we have a valid group ID.
if ( ! $code_group_id || ! is_numeric( $code_group_id ) ) {
return;
}
// Example: Log the creation of the code group.
error_log( sprintf(
'New code group created: ID %1$s for user %2$s. Attributes: %3$s',
$code_group_id,
$attr['user_id'] ?? 'N/A',
print_r( $attr, true )
) );
// Example: You could also retrieve user details based on $attr['user_id']
// and send a custom email notification using wp_mail().
$user_id = isset( $attr['user_id'] ) ? absint( $attr['user_id'] ) : 0;
if ( $user_id ) {
$user = get_userdata( $user_id );
if ( $user ) {
$subject = 'Your new code group has been created!';
$message = sprintf(
'Hello %1$s, nn A new code group named "%2$s" has been successfully created for you. nn Regards, n Your Website Team',
$user->display_name,
$attr['group_name'] ?? 'Unnamed Group'
);
// wp_mail( $user->user_email, $subject, $message );
}
}
// This is an action hook, so no return value is expected or used.
}
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/handlers/class-group-management-db-handler.php:175
public function add_code_group( $attr ) {
global $wpdb;
if ( ! ulgm()->db->if_table_exists( ulgm()->db->tbl_group_details ) ) {
ulgm()->db->create_tables();
}
$insert = array(
'user_id' => absint( $attr['user_id'] ),
'order_id' => absint( $attr['order_id'] ),
'product_id' => isset( $attr['product_id'] ) ? absint( $attr['product_id'] ) : 0,
'ld_group_id' => absint( $attr['group_id'] ),
'group_name' => $attr['group_name'],
'issue_date' => current_time( 'mysql' ),
);
$wpdb->insert(
$wpdb->prefix . ulgm()->db->tbl_group_details,
$insert,
array(
'%d',
'%d',
'%d',
'%s',
'%s',
)
);
$code_group_id = $wpdb->insert_id;
do_action( 'ulgm_code_group_created', $code_group_id, $attr );
return $code_group_id;
}