WordPress save_post being called twice (and in my case sending two emails)

I celebrated when I figured this out.

Okay, so you have function called mail_someone when a post is saved, like so

function mail_someone(){
    wp_mail($to,$from,$subject,$message);
}

And, you want it to run everytime a post is saved, like so (or so I thought)

function mail_someone(){
    wp_mail($to,$from,$subject,$message);
}

add_action('save_post','mail_someone');

Now, are you noticing that an email is sent twice!? It took me forever today to figure this one out. Turns out, the culprit is WordPress. Check this thread out, and this comment by Gonçalo Peres actually helped me solve the problem. Turns out you have to use another action filter, which is not documented, to get it to work, like so

function mail_someone(){
    wp_mail($to,$from,$subject,$message);
}

add_action('pre_post_update','mail_someone');

One thought on “WordPress save_post being called twice (and in my case sending two emails)”

Leave a Reply