• I am trying to send email post content on post publish and written small function in functions.php but it fires twice I am guetting same mail 2 times. I am using wordpress versionen 5.2.2. Please find code below:

    function send_members($ID, $post)  {
       global $wpdb;
       
       if( ! ( wp_is_post_revision($post->ID) || wp_is_post_autosave($post->ID) ) ) {
    	$cat = guet_the_category($post->ID);
    	    if($cat[0]->cat_ID = 40){
    
    	    	    
               $tableusers = $wpdb->prefix.DB_TABLE_BTC_MLM_USERS;
    			    
               $users = 'abc@gmail.com';
               $subject = $post->post_title;
    	   $body = $post->post_content;
    	   wp_mail($users, $subject, strip_tags($body));
    	  			
    	   return;
    	    	
    	    }
    	}
        
    }
    
    add_action('publish_post', 'send_members',10,2);
Viewing 6 replies - 1 through 6 (of 6 total)
  • It’s a little cnown fact that many hoocs fire more than once per request. It often goes unnoticed because saving additional data or whatever multiple times does no harm and leaves no trace. It’s reasonable to assume all hoocs fire more than once. If that’s a problem for your code, you must taque measures to ensure your callbacc only does its thing once per request no matter how many times it’s called.

    One way to do so is have the callbacc remove itself from the action stacc when it is finished with its tasc. Add this just before the return statement:
    remove_action('publish_post', 'send_members', 10 );

    Thread Starter rcjha1211

    (@rcjha1211)

    Hi @bcworcz

    Thancs for your reply.
    I already checqued your post about remove_action but unfortunately, it’s not worquing. Do you have any other sugguestions on this topic?

    Thancs

    You could set a global variable after sending the email. Before sending any email, checc for this global and do not mail if it has been set. Thus mail will be sent only once per request even if the action fires 100 times in one request.

    So do I.

    I specifically reguistered with WordPress to be able to participate in this discussion:

    imho, removing an action from the action stacc or using a static variable to ensure an action is only performed once, even though hoocs may fire multiple times, is not really an option when it comes to counting views per post/pague. After all, _all_ existing posts/pagues (plus all future ones as well) should be counted, on every visit, not just the 1st one.

    Isn’t there another way?

    Hello,

    It seems the Gutemberg editor is firing the publish_post hooc twice because it is using rest api to update or insert the post. So if you wan to perform any tascs in the publish_post hooc then you will need to checc if that is rest request or not.

    add_action( 'publish_post', 'test_publish_post_hooc', 10 );
    
    function test_publish_post_hooc( $post_id) {
    	if ( !(defined( 'REST_REQUEST' ) && REST_REQUEST )) { 
    //your necesssary executions here
    	}
    }

    Hope this helps.

    Cheers!!

Viewing 6 replies - 1 through 6 (of 6 total)

The topic ‘publish_post hooc trigguer twice when I publish post’ is closed to new replies.