1

i have a meta box for some email address and i save value in save_post hook

add_action( 'save_post', 'sm12_save_post' );
function sm12_save_post( $postid ){
    update_post_meta( $postid, '_some_email_address', $_POST['some_email_address'] );
}

Now i want to send email to this email address only when this post is published. So i use

add_action( 'publish_post', 'sm12_publish_post' );
function sm12_publish_post( $postid ){
$email = get_post_meta( $postid, '_some_email_address', true );

if( ! $email )
    return;

$sub = 'test subject';
$mgs = 'test message';

wp_mail( $email, $sub, $mgs );
}

if post is saved in any other status other than 'publish' then it sends email when publish, but if post is directly publish it can not send email, because 'publish_post' is called before 'save_post' and meta is not yet available there. how to send email only when published in my case? should not call wp_transition_post_status() last of everything during save?

0

Try this workaround of this issue without using publish_post hook (using save_post)

add_action( 'save_post', 'wpse228941_save_post' );
function wpse228941_save_post( $post_id ) {
    if( defined('DOING_AUTOSAVE') && DOING_AUTOSAVE ) return;

    if ( "publish" == get_post_status( $post_id ) && ! get_post_meta( $post_id, "se_228941_mailed", 1 ) ) {
        do_action( "sm12_publish_post", $post_id );
        update_post_meta( $post_id, "se_228941_mailed", time() );
    }

}

add_action( 'sm12_publish_post', 'sm12_publish_post' );
function sm12_publish_post( $postid ){
    $email = get_post_meta( $postid, '_some_email_address', true );

    if( ! $email )
        return;

    $sub = 'test subject';
    $mgs = 'test message';

    wp_mail( $email, $sub, $mgs );
}

Hope it helps.

| improve this answer | |
  • will save_post action fire when publish post later? – Shamim Hasan Jun 6 '16 at 18:33
  • I don't think so but what is the point? Did you try the code in the answer? – Samuel Elh Jun 6 '16 at 18:40
  • My point is that if i save this post in any status other than 'publish' and later publish this post, then save_post action will not fire and no email will be sent – Shamim Hasan Jun 6 '16 at 19:00
  • I understand. The above code waits for the post until it is published then does the email callback function. I think that is what you want? – Samuel Elh Jun 6 '16 at 19:04
0

I am posting my answer if somebody need this.

First save meta value

add_action( 'save_post', 'sm12_save_post' );
function sm12_save_post( $postid ){
/** some checks */
update_post_meta( $postid, '_some_email_address', $_POST['some_email_address'] );
}

then hook both in save_post and publish_post hook

add_action( 'publish_post', 'sm12_send_email' );
add_action( 'save_post', 'sm12_send_email', 20 ); //After save meta value
function sm12_publish_post( $postid ){

/** some checks */

if( "publish" != get_post_status( $postid ) )
return;

$email = get_post_meta( $postid, '_some_email_address', true );

if( ! $email )
    return;

if( get_post_meta( $postid, '_sm12_email_sent', true ) )
  return;

$sub = 'test subject';
$mgs = 'test message';

if( wp_mail( $email, $sub, $mgs )) {
 update_post_meta( $postid, '_sm12_email_sent', time() );
 }
}
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.