我有一个名为“自动”的cpt,我制作了一个前端表单来创建状态为“待定”的新帖子。在提交帖子后,我会收到一封电子邮件,其中包含新帖子的通知。
function newpost_notify() {
$mailadmin = \'xxx@domain.com\';
$subject = \'Subject\';
$headers = \'MIME-Version: 1.0\' . "\\r\\n";
$headers .= \'Content-type: text/html; charset=UTF-8\' . "\\r\\n";
$headers .= \'From: xxx <noreply@domain.com>\' . "\\r\\n";
$message = \'There\\\'s a new post.\';
wp_mail( $mailadmin, $subject, $message, $headers );
}
add_action( \'publish_post\', \'newpost_notify\', 10, 2 );
这是我的代码。。但我没有收到任何电子邮件。
我会知道“新帖子”和“发布帖子”之间是否有区别,因为我读了一些关于帖子状态转换的内容。
非常感谢。
最合适的回答,由SO网友:sb0k 整理而成
编辑-解决方案
我使用状态“new\\u to\\u pending”并添加$auto对象获得了解决方案。
function newpost_notify( $auto ) {
$mailadmin = \'xxx@domain.com\';
$subject = \'Subject\';
$headers = \'MIME-Version: 1.0\' . "\\r\\n";
$headers .= \'Content-type: text/html; charset=UTF-8\' . "\\r\\n";
$headers .= \'From: xxx <noreply@domain.com>\' . "\\r\\n";
$message = \'There\\\'s a new post.\';
wp_mail( $mailadmin, $subject, $message, $headers );
}
add_action( \'new_to_pending\', \'newpost_notify\', 10, 2 );
感谢@Rarst&;的建设性意见@弗洛米
SO网友:emersonthis
公认的答案在2021对我来说并不适用。我怀疑动作挂钩名称已更改。
以下是对我有效的更新解决方案:
function newpost_notify( $auto ) {
$mailadmin = \'xxx@domain.com\';
$subject = \'Subject\';
$headers = \'MIME-Version: 1.0\' . "\\r\\n";
$headers .= \'Content-type: text/html; charset=UTF-8\' . "\\r\\n";
$headers .= \'From: xxx <noreply@domain.com>\' . "\\r\\n";
$message = \'There\\\'s a new post.\';
wp_mail( $mailadmin, $subject, $message, $headers );
}
add_action( \'pending_auto\', \'newpost_notify\', 10, 2 );
Note that the action name is dynamic, so you will need to adjust the first argument of add_action
to match your custom post_type. 格式为{status}{postType}。
还要注意,传递给回调函数的参数是新post的post ID(不是WP\\U post的实例)。