You were close. The hook, you’re trying to use, is called from function wp_transition_post_status()
:
function wp_transition_post_status( $new_status, $old_status, $post ) {
//
// ...
//
do_action( "{$new_status}_{$post->post_type}", $post->ID, $post );
}
So, to execute code when product status changes add function to hook publish_product
.
The first argument passed to the function is $post_id
, not WP_Post
object.
Last thing, checking the type of post in your function on_publish_pending_post
is unnecessary, because hook is for a specific type ( “publish_post“, “publish_product” ).
Try this code:
add_action("publish_product", "on_publish_pending_post", 10, 2);
function on_publish_pending_post($post_id, $post) {
$auth_id = (int)$post->post_author;
$user = get_userdata($auth_id);
if ( $user === false )
return;
$to_email = $user->user_email;
$name = get_the_title($post_id);
$fromMail = "[email protected]";
$subjectMail = "Design Approval Success";
$content="<p>Your Design has been published !!!</p>";
$headersMail="";
$headersMail .= 'From: ' . $fromMail . "\r\n";
$headersMail .= 'Reply-To: ' . $fromMail . "\r\n";
$headersMail .= 'MIME-Version: 1.0' . "\r\n";
$headersMail .= 'Content-Type: text/html; charset=ISO-8859-1' . "\r\n";
// ------
// echo "<br>recipient: " . htmlspecialchars($to_email);
// echo "<br>sender: " . htmlspecialchars($fromMail);
// echo "<br>post_title: " . htmlspecialchars($name);
// exit();
// ------
mail($to_email, $subjectMail, $content, $headersMail);
}