用户在wordpress中提交帖子后发送邮件

Ell*_*lle 0 php email wordpress


在我的网站上,用户可以通过普通表格提交帖子。
我保存的PHP变量是:

$usersarray = store the mail of the user
$postId = store the ID of the submitted post
Run Code Online (Sandbox Code Playgroud)

提交的帖子会自动输入“ 草稿 ”中。
我想在他的帖子变为“已发布 ” 时自动发送邮件,该怎么办?

当然,这必须与不同的用户和提交的不同帖子一起使用,而不仅仅是最后一个。(每个用户都提交了他的帖子)

感谢您的建议。

Mas*_*ile 5

使用publish_post挂钩,该挂钩是在帖子更新且其新状态为“发布”时触发的动作。

在文章发表后通过wp_mail()发送电子邮件给帖子作者。

function post_published_notification( $ID, $post ) {
    $author = $post->post_author; /* Post author ID. */
    $name = get_the_author_meta( 'display_name', $author );
    $email = get_the_author_meta( 'user_email', $author );
    $title = $post->post_title;
    $permalink = get_permalink( $ID );
    $edit = get_edit_post_link( $ID, '' );
    $to[] = sprintf( '%s <%s>', $name, $email );
    $subject = sprintf( 'Published: %s', $title );
    $message = sprintf ('Congratulations, %s! Your article “%s” has been published.' . "\n\n", $name, $title );
    $message .= sprintf( 'View: %s', $permalink );
    $headers[] = '';
    wp_mail( $to, $subject, $message, $headers );
}
add_action( 'publish_post', 'post_published_notification', 10, 2 );
Run Code Online (Sandbox Code Playgroud)

编辑:

正如@Stender在下面的评论中建议的那样。

您也可以使用

draft_to_publish 钩子将完美满足您的需求。