ulgm_seats_added
Fires when new seats are successfully added to a LearnDash group, providing details about the action.
add_action( 'ulgm_seats_added', $callback, 10, 5 );
Description
Fires after new seat codes are successfully added to a LearnDash group. Developers can use this hook to perform custom actions, such as updating user roles, sending notifications, or modifying related metadata, based on the added codes and group information.
Usage
add_action( 'ulgm_seats_added', 'your_function_name', 10, 5 );
Parameters
-
$count(mixed) - This parameter contains the number of seats that were added.
-
$ld_group_id(mixed) - This parameter represents the number of seats that have been successfully added.
-
$code_group_id(mixed) - This parameter contains the ID of the LearnDash group associated with the seats being added.
-
$attr(mixed) - This parameter contains the ID of the code group to which seats are being added.
-
$codes(mixed) - This parameter contains an array of attributes related to the group management process, including the `code_group_id`.
Examples
// Hook into the 'ulgm_seats_added' action to perform actions after new group codes are added.
// This function will log the number of seats added and the associated group IDs.
add_action( 'ulgm_seats_added', 'my_log_new_group_seats', 10, 5 );
/**
* Logs the details of newly added group seats.
*
* @param int $count The number of seats (codes) added.
* @param int $ld_group_id The LearnDash Group ID associated with the codes.
* @param int $code_group_id The internal ID for the group of codes.
* @param array $attr The attributes used when adding the codes.
* @param array $codes An array of the generated codes.
*/
function my_log_new_group_seats( $count, $ld_group_id, $code_group_id, $attr, $codes ) {
// Ensure we have a valid count and group IDs before proceeding.
if ( ! $count || ! $code_group_id ) {
return;
}
// Construct a log message.
$log_message = sprintf(
'New group seats added: %d seats added to code group ID %d (LearnDash Group ID: %d). Quantity requested: %d.',
$count,
$code_group_id,
$ld_group_id,
isset( $attr['qty'] ) ? absint( $attr['qty'] ) : 'N/A'
);
// Optionally, log the actual codes if desired (be mindful of PII).
// For this example, we'll skip logging the actual codes for security.
// If you needed to log them, you might do:
// $log_message .= ' Codes: ' . implode( ', ', $codes );
// Use WordPress's error logging functions to record the event.
// error_log( $log_message );
// Alternatively, you could store this information in a custom database table
// or trigger other notifications based on the `$count`, `$ld_group_id`, etc.
// Example: If the number of seats added exceeds a certain threshold, send an email notification.
if ( $count > 50 ) {
$admin_email = get_option( 'admin_email' );
$subject = sprintf( 'High Volume of Group Seats Added: %d', $count );
$body = $log_message . "nn" . 'Details: ' . print_r( $attr, true );
wp_mail( $admin_email, $subject, $body );
}
}
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:268
public function add_additional_codes( $attr, $codes ) {
global $wpdb;
$code_group_id = absint( $attr['code_group_id'] );
$table = $wpdb->prefix . ulgm()->db->tbl_group_codes;
$codes_q = "INSERT INTO $table ( `group_id`, `code`, `code_status` ) VALUES ";
$group_id = absint( $code_group_id );
for ( $i = 0; $i < $attr['qty']; $i++ ) {
$code = $codes[ $i ];
$codes_q .= "( $group_id, '$code', '" . SharedFunctions::$available_status . "' ),";
}
$q = rtrim( $codes_q, ',' );
$r = $wpdb->query( $q );
$count = count( $codes );
$ld_group_id = ulgm()->group_management->ld_group_id_from_code_group_id( $code_group_id );
do_action( 'ulgm_seats_added', $count, $ld_group_id, $code_group_id, $attr, $codes );
return $r;
}