你很接近。您试图使用的挂钩是从函数调用的wp_transition_post_status()
:
function wp_transition_post_status( $new_status, $old_status, $post ) {
//
// ...
//
do_action( "{$new_status}_{$post->post_type}", $post->ID, $post );
}
因此,要在以下情况下执行代码:
product 状态更改将函数添加到挂钩
publish_product
.
传递给函数的第一个参数是
$post_id
, 不
WP_Post
对象
最后一件事,检查功能中的帖子类型
on_publish_pending_post
没有必要,因为挂钩用于特定类型(“发布_
post“,”发布_
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);
}