我想在新的woo产品发布后发送电子邮件。收件人电子邮件被添加到产品中的自定义元内。到目前为止,以下是我在functions.php中拥有的内容。如果我在该功能中添加了手动电子邮件,则可以正常工作,但是很遗憾,我无法获取该帖子的元数据。这是因为正在/尚未保存产品时触发了功能吗?我该如何实现?
function set_mail_html_content_type() {
return 'text/html';
}
function notify_owner($post_id) {
if ( get_post_type($post_id) == 'product' ) {
$post = get_post($post_id);
$owner_email = get_post_meta($post_id, 'my_custom_meta_here', true);
$subject = "Hello";
$message = "<p>Message goes here.</p>";
add_filter('wp_mail_content_type', 'set_mail_html_content_type');
wp_mail($owner_email, $subject, $message);
remove_filter('wp_mail_content_type', 'set_mail_html_content_type');
}
}
add_action('pending_to_publish', 'notify_owner');
add_action('draft_to_publish', 'notify_owner');
add_action('future_to_publish', 'notify_owner');
add_action('private_to_publish', 'notify_owner');
add_action('auto-draft_to_publish', 'notify_owner');
我也尝试添加/更改以下运气:
global $post;
$owner_email = get_post_meta($post->ID, 'my_custom_meta_here', true);
$owner_email = get_post_meta(get_the_ID(), 'my_custom_meta_here', true);
答案 0 :(得分:1)
用于回调函数的转换后状态操作钩子将$post
用作WP_post
对象,但是您使用的是$post_id
,这是错误的。因此,对于您的情况,您可以使用以下选项:
1。。实施{$old_status}_to_{$new_status}
动作挂钩。
function set_mail_html_content_type() {
return 'text/html';
}
function notify_owner($post) {
if ( $post->post_type == 'product' ) {
$owner_email = get_post_meta($post->ID, 'my_custom_meta_here', true);
$subject = "Hello";
$message = "<p>Message goes here.</p>";
add_filter('wp_mail_content_type', 'set_mail_html_content_type');
wp_mail($owner_email, $subject, $message);
remove_filter('wp_mail_content_type', 'set_mail_html_content_type');
}
}
add_action('pending_to_publish', 'notify_owner');
add_action('draft_to_publish', 'notify_owner');
add_action('future_to_publish', 'notify_owner');
add_action('private_to_publish', 'notify_owner');
add_action('auto-draft_to_publish', 'notify_owner');
2。。实现{$new_status}_{$post->post_type}
动作挂钩
function set_mail_html_content_type() {
return 'text/html';
}
function notify_owner($post) {
$owner_email = get_post_meta($post->ID, 'my_custom_meta_here', true);
$subject = "Hello";
$message = "<p>Message goes here.</p>";
add_filter('wp_mail_content_type', 'set_mail_html_content_type');
wp_mail($owner_email, $subject, $message);
remove_filter('wp_mail_content_type', 'set_mail_html_content_type');
}
add_action('publish_product', 'notify_owner');