Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save AngeliMae/d07a3f925005b0a5e757f6bdc119597e to your computer and use it in GitHub Desktop.

Select an option

Save AngeliMae/d07a3f925005b0a5e757f6bdc119597e to your computer and use it in GitHub Desktop.
Disable Welcome Email After Registration for Certain Roles
add_action( 'um_before_email_notification_sending', function( $email, $template, $args ) {
if ( 'custom-role-id' === um_user( 'role' ) && 'welcome_email' === $template ) {
add_filter( 'pre_wp_mail', '__return_true' );
}
}, 10, 3 );
add_action( 'um_after_email_notification_sending', function( $email, $template, $args ) {
if ( 'custom-role-id' === um_user( 'role' ) && 'welcome_email' === $template ) {
remove_filter( 'pre_wp_mail', '__return_true' );
}
}, 10, 3 );
@nikitasinelnikov

nikitasinelnikov commented Apr 19, 2023

Copy link
Copy Markdown

Since 2.6.1 version you may use this code snippet to avoid WordPress native 'pre_wp_mail' hook:

add_filter( 'um_disable_email_notification_sending', 'my_um_disable_email_notification_sending', 10, 4 );
function my_um_disable_email_notification_sending( $disable, $email, $template, $args ) {
    $user = get_user_by( 'email', $email );
    um_fetch_user( $user->ID );
    if ( UM()->user()->get_role() == 'custom-role-id' && 'welcome_email' == $template ) {
        $disable = true;
    }
    return $disable;
}

@yuriinalivaiko

yuriinalivaiko commented Apr 19, 2023

Copy link
Copy Markdown

Hi @AngeliMae
Your code may work wrong. The um_before_email_notification_sending hook is an action, not a filter. This hook has only 3 parameters.
Try this code instead:

// Disable the Account Welcome Email for Contributor.
add_action( 'um_before_email_notification_sending', function( $email, $template, $args ) {
	if ( 'custom-role-id' === um_user( 'role' ) && 'welcome_email' === $template ) {
		add_filter( 'pre_wp_mail', '__return_true' );
	}
}, 10, 3 );
add_action( 'um_after_email_notification_sending', function( $email, $template, $args ) {
	if ( 'custom-role-id' === um_user( 'role' ) && 'welcome_email' === $template ) {
		remove_filter( 'pre_wp_mail', '__return_true' );
	}
}, 10, 3 );

This code also can disable an email for a role:

// Disable the Account Welcome Email for Contributor v2.0.
add_filter( 'um_get_option_filter__welcome_email_on', function( $option ) {
	if ( 'custom-role-id' === um_user( 'role' ) ) {
		$option = false;
	}
	return $option;
} );

@AngeliMae

Copy link
Copy Markdown
Author

It is working now, Thank you!

@nikitasinelnikov

Copy link
Copy Markdown

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment