Send email only upon draft

时间:2012-07-03 作者:erichmond

我正在尝试仅在将帖子保存为草稿时发送电子邮件,这似乎与当前代码不兼容:

add_action( \'save_post\', \'er_send_email_on_post_draft_save\' );

function er_send_email_on_post_draft_save( $post_id ) {

    //verify post is not a revision
    if ( $post_id->post_status == \'draft\' ) {

        $post_title = get_the_title( $post_id );
        $post_url = get_permalink( $post_id );
        $subject = \'A post has been updated\';

        $message = "A post has been updated on your website:\\n\\n";
        $message .= "" .$post_title. "\\n\\n";

        //send email to admin
        wp_mail( \'[email protected]\', $subject, $message );

    }
}
如果我将if语句更改为:

!wp_is_post_revision( $post_id )
但这不是我想要的,我只想在只保存为草稿时发送通知。

2 个回复
最合适的回答,由SO网友:Bainternet 整理而成

$post_id 是一个整数(只是post id),而不是post对象(包含id、status、title等的整个post)

因此,全球化$post 对象并从此处检查状态,例如:

function er_send_email_on_post_draft_save( $post_id ) {
    global $post;
    //verify post is not a revision
    if ( $post->post_status == \'draft\' ) {

        $post_title = get_the_title( $post_id );
        $post_url = get_permalink( $post_id );
        $subject = \'A post has been updated\';

        $message = "A post has been updated on your website:\\n\\n";
        $message .= "" .$post_title. "\\n\\n";

        //send email to admin
        wp_mail( \'[email protected]\', $subject, $message );

    }
}
如果你想的话,你可以用钩子only when post is saved as draft

add_action(\'draft_post\', \'send_my_mail_on_draft\' );
function send_my_mail_on_draft( $post_id,$post){
   $post_title = get_the_title( $post_id );
   $post_url = get_permalink( $post_id );
   $subject = \'A post has been updated\';

   $message = "A post has been updated on your website:\\n\\n";
   $message .= "" .$post_title. "\\n\\n";

   //send email to admin
   wp_mail( \'[email protected]\', $subject, $message );
}

SO网友:Eager2Learn

尝试以下操作:

function dddn_process($id) {

    // emails anyone on or above this level
    $email_user_level = 7;

    global $wpdb;

    $tp = $wpdb->prefix;

    $result = $wpdb->get_row("
        SELECT post_status, post_title, user_login, user_nicename, display_name 
        FROM {$tp}posts, {$tp}users 
        WHERE {$tp}posts.post_author = {$tp}users.ID 
        AND {$tp}posts.ID = \'$id\'
    ");

    if (($result->post_status == "draft") || ($result->post_status == "pending")) {

        $message = "";
        $message .= "Draft updated on \'" . get_bloginfo(\'name\') . "\'\\n\\n";
        $message .= "Title: " . $result->post_title . "\\n\\n";

            // *** Choose one of the following options to show the author\'s name

        $message .= "Author: " . $result->display_name . "\\n\\n";
        // $message .= "Author: " . $result->user_nicename . "\\n\\n";
        // $message .= "Author: " . $result->user_login . "\\n\\n";

        $message .= "Link: " . get_permalink($id);

        $subject = "Draft updated on \'" . get_bloginfo(\'name\') . "\'";


        $editors = $wpdb->get_results("SELECT user_id FROM {$tp}usermeta WHERE {$tp}usermeta.meta_value >= " . $email_user_level);

        $recipient = "";    

        foreach ($editors as $editor) {         
            $user_info = get_userdata($editor->user_id);
            $recipient .= $user_info->user_email . \',\'; 
        } 

        mail($recipient, $subject, $message);


    }

}


add_action(\'save_post\', \'dddn_process\');

?>
只需创建通知。带有wp插件描述的php文件将其上载到插件文件夹并作为常规插件激活

结束

相关推荐

Extending post by email

只是试探一下这一点,我需要能够接受收到的电子邮件并将其添加为帖子,但我希望能够首先进行一些处理,例如我想更改slug,在标题前加一个字符串等。是否有一种方法可以连接到电子邮件功能,更改一些数据,然后让它继续。如果电子邮件不是从批准的域发送的,我可能还想拒绝它(这将是自定义帖子类型中的一个字段)。我的要求是可以实现的,还是我找错了方向?Edit我发现了save_post 钩子,虽然它在帖子保存后开火。。。不是我想要的。是否有一个钩子在帖子保存之前被激活,它会给我关于帖子的所有信息,尤其是创建帖子的地方。Ed