我正在为已批准的帖子创建通知电子邮件。它向作者发送邮件,如“您的帖子已批准”。所以我使用publish_post
filder公司。
function notificationApprove( $ID, $post ) {
// Send mail
}
add_action( \'publish_post\', \'notificationApprove\', 10, 2 );
它工作得很好。但问题是,它会发送每篇帖子保存的邮件。我只想发送等待批准的交换机状态的邮件。我正在为已批准的帖子创建通知电子邮件。它向作者发送邮件,如“您的帖子已批准”。所以我使用publish_post
filder公司。
function notificationApprove( $ID, $post ) {
// Send mail
}
add_action( \'publish_post\', \'notificationApprove\', 10, 2 );
它工作得很好。但问题是,它会发送每篇帖子保存的邮件。我只想发送等待批准的交换机状态的邮件。您可以使用transition_post_status
钩子,每当状态更改时都会激发它。因此,当状态更改为publish
, 您可以这样做:
function notificationApprove( $new_status, $old_status, $post ) {
if ( \'publish\' === $new_status && \'publish\' !== $old_status && \'post\' === $post->post_type ) {
// Send mail
}
}
add_action( \'transition_post_status\', \'notificationApprove\', 10, 3 );
这将仅在状态更改时激发。这意味着,如果帖子未发布,然后重新发布,则会再次发送通知。如果只发送一次通知,则可以在发送通知时保存自定义字段,然后在重新发布帖子时检查该字段是否已存在,如果存在,则不发送通知:
function notificationApprove( $new_status, $old_status, $post ) {
if ( \'publish\' === $new_status && \'publish\' !== $old_status && \'post\' === $post->post_type ) {
$notification_sent = get_post_meta( $post->ID, \'notification_sent\', true );
if ( $notification_sent !== \'sent\' ) {
// Send mail
update_post_meta( $post->ID, \'notification_sent\', \'sent\' );
}
}
}
add_action( \'transition_post_status\', \'notificationApprove\', 10, 3 );
我已在本地设置Wordpress,我正在尝试使用此功能发送电子邮件:$key = get_password_reset_key($user); $url = \'http://frontend.web.node.net/reset-password?token=\' . $key; $to = $param[\'email\']; $subject = \'Passord reset\'; $body = \'Du kan tilbakestille passordet d