【问题标题】:Send mail after user submit his post in wordpress用户在 wordpress 中提交帖子后发送邮件
【发布时间】:2018-12-20 13:19:22
【问题描述】:


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

$usersarray = store the mail of the user
$postId = store the ID of the submitted post

提交的帖子自动进入“草稿”。
我想在他的帖子变成“Published”时自动发送邮件,我该怎么做?

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

感谢您的建议。

【问题讨论】:

    标签: php wordpress email


    【解决方案1】:

    使用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 );
    

    编辑:

    正如@Stender 在下面的 cmets 中所建议的那样。

    你也可以使用

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

    【讨论】:

    • 旁注:draft_to_publish 可以使用钩子,如果他不想发送其他帖子。这样你就可以忽略future, private, auto-draft, pending 的帖子等 - 但是是的 - 完美的方式来做到这一点
    • @Stender 只是为了理解...我必须将draft_to_publish 放入add_action( 'publish_post', 'post_published_notification', 10, 2 ); 以便它变成add_action( 'draft_to_publish', 'post_published_notification', 10, 2 );
    • @Elle 是这样的
    • 是的 - 把它全部扔到你的functions.php
    • @MasivuyeCokile 一切正常!我使用了get_post_field 而不是get_the_author_meta
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-04-02
    • 2015-03-28
    • 2016-07-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多