ulgm_maybe_send_welcome_email
Filters whether a welcome email should be sent to a user after joining a group.
add_filter( 'ulgm_maybe_send_welcome_email', $callback, 10, 3 );
Description
Filters whether to send the welcome email to a new group member. Developers can use this to conditionally prevent the welcome email from being sent based on specific criteria, like user role or group type. Defaults to true.
Usage
add_filter( 'ulgm_maybe_send_welcome_email', 'your_function_name', 10, 3 );
Parameters
-
$to(mixed) - This parameter likely indicates whether the welcome email should be sent, acting as a boolean flag.
-
$subject(mixed) - This parameter represents the recipient of the welcome email.
-
$group_id(mixed) - This parameter contains the subject line of the welcome email.
Return Value
The filtered value.
Examples
<?php
/**
* Prevent sending welcome email to a specific user based on group ID.
*
* This filter hook allows developers to conditionally prevent the welcome email
* from being sent. In this example, we'll prevent the email from being sent
* if the user is being added to a specific "Premium Members" group (group ID 42).
*
* @param bool $send_mail The default value, indicating whether to send the email.
* @param mixed $to The recipient of the email.
* @param mixed $subject The subject of the email.
* @param int $group_id The ID of the group the user is being added to.
* @return bool Returns false to prevent the email from sending, otherwise true.
*/
add_filter( 'ulgm_maybe_send_welcome_email', function( $send_mail, $to, $subject, $group_id ) {
// Define the group ID for the "Premium Members" group.
$premium_members_group_id = 42;
// If the group ID matches our premium group, prevent sending the welcome email.
if ( (int) $group_id === $premium_members_group_id ) {
// Log that we are preventing the email for this group.
error_log( "Preventing welcome email for group ID {$group_id} as it's a premium group." );
return false; // Prevent the email from being sent.
}
// For all other groups, allow the email to be sent as usual.
return $send_mail;
}, 10, 4 );
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/class-group-management-helpers.php:1385
$subject = apply_filters( 'ulgm_welcome_email_subject', $welcome_template_subject, $user_data, $group_name );
$body = apply_filters( 'ulgm_welcome_email_body', $welcome_template_body, $user_data, $group_name );
if ( ! class_exists( 'WP_Better_Emails' ) || ( false === preg_match( '/<DOCTYPE/', $body ) && false === preg_match( '/<head>/', $body ) ) ) {
$body = wpautop( $body );
}
$send_mail = apply_filters( 'ulgm_maybe_send_welcome_email', true, $to, $subject, $group_id );
if ( $send_mail ) {
$mail_sent = SharedFunctions::wp_mail( $to, $subject, $body, self::get_headers() );
return $mail_sent;
} else {
return true;
}