WordPress disable email notifications programmatically (php fix) - React to WordPress

WordPress disable email notifications programmatically (php fix)

A simple solution to a complicated issue – the spamming of innocent members with unuseful emails

How to disable all WordPress emails modularly and programmatically?

Option 1: Remove the ‘to’ argument from wp_mail function in WordPress, it will keep your system running without sending any default WordPress emails.

add_filter('wp_mail','disabling_emails', 10,1);
function disabling_emails( $args ){
    unset ( $args['to'] );
    return $args;
}

The wp_mail is a wrapper for the phpmailer class and it will not send any emails if there is no recipient.

Option 2: Hook to the phpmailer class directly and ClearAllRecipients from there

function react2wp_clear_recipients( $phpmailer ) {
    $phpmailer->ClearAllRecipients();
}
add_action( 'phpmailer_init', 'react2wp_clear_recipients' );

Now we removed all the recipients directly from the core class of $phpmailer.

Option 3: Keep using wp_mail for your own needs but disable for everything else.

add_filter('wp_mail','disabling_emails', 10,1);
function disabling_emails( $args ){
    if ( ! $_GET['allow_wp_mail'] ) {
        unset ( $args['to'] );
    }
    return $args;
}

and when you are calling wp_mail use it like this:

$_GET['allow_wp_mail'] = true;
wp_mail( $to, $subject, $message, $headers );
unset ( $_GET['allow_wp_mail'] ); // optional

That’s it.

Best WordPress hosting? SiteGround. Cheap, Fast and Friendly to Developers! 3.95$/Month

Leave a Reply

Your email address will not be published. Required fields are marked *