When the posts are registered as draft, I get 2 notifications to my email address. Actually I should get 1 notification


I wrote a code to get notified when a post is made on the site or when a post is saved as a draft. It works but sends 2 mail instead of 1. I tried it in its simplest form, but it didn’t improve. I disabled the plugins. It didn’t improve. What could be the reason for this?

function on_all_status_transitions( $new_status, $old_status, $post ) {
    if ( $new_status != $old_status || $new_status == $old_status ) {
        if ($new_status == 'draft') {
            $subject = 'Draft';
            $body = 'Old Status: '.$old_status.'<br>New Status: '.$new_status;
        $admin_email = get_option( 'admin_email' );
        $to = $admin_email ;
        $headers = array('Content-Type: text/html; charset=UTF-8');
        wp_mail( $to, $subject, $body, $headers );
add_action(  'transition_post_status',  'on_all_status_transitions', 10, 3 );

I uploaded this code to the theme’s function file.

Actually I added ID, name, title etc.. but it sends 2 notifications even in the simplest form. Suggestion, solution, reason..? I am waiting for your answers.

Mert VAROL 2 years 2020-10-25T13:10:24-05:00 0 Answers 10 views 0

Leave an answer