0
votes

I'm working on a Wordpress / Woocommerce site where "Advertisers" can publish their own products.

There's a 2 step process to publishing their product, so when a user submits their product the status is set to "Pending Review", I've added a new publish status "Final Validation", Is there a way I can use something like the publish_post hook that I can use to email the author when we change the Publish status to "Final Validation"? I figured I would be able to easily edit the code below to just say "If status change = "Final Validation do this" but Im not really getting anywhere. Obviously the add_action shouldn't be "publish_post", but I'm not sure what it should be.

Can anyone help?

function notifyauthor($post_id) {
 
$post = get_post($post_id);
$author = get_userdata($post->post_author);
$subject = "We are validating your post : ".$post->post_title."";
 
$message = "
      Hi ".$author->display_name.",
       
      Your product, \"".$post->post_title."\" is now in the final stages of being validated.
       
      View post: ".get_permalink( $post_id )."
       
      Thanks"
      ;
       
   wp_mail($author->user_email, $subject, $message);
}
add_action('publish_post', 'notifyauthor');
1
OK, I think I need to use : do_action( "{$old_status}_to_{$new_status}", $post ); but I don't really understand it, sorry, I'm not good with PHP - sleepyjones

1 Answers

0
votes

Managed to get this working. Will fire off an email if the Publish status changes from "Pending Review" to "Verified".

in the functions file


function authorNotification( $new_status, $old_status, $post ) {
    if ( $new_status == 'verified' && $old_status != 'pending review' ) {
        $headers = 'From: MyName <[email protected]>' . "\r\n";
        $author = get_userdata($post->post_author);
        $message = "
            Hello ".$author->display_name.",
            We have verified your offer and are ready to publish:  ".$post->post_title." 
            
        ";
        wp_mail($author->user_email, "We have verified your offer", $message, $headers);
    }
}
add_action('transition_post_status', 'authorNotification', 10, 3 );