【问题标题】:Send mail after publish post on wordpress在 wordpress 上发布帖子后发送邮件
【发布时间】:2013-12-04 19:35:16
【问题描述】:

在我的网站上发布帖子后,我正在编写一个发送邮件的函数,但问题如下:

如果我编辑已发布的帖子,则每次我更新已发布的帖子时都会发送一封新邮件。

这是我写的函数:

function send_mails($post_ID)  {
     global $wpdb;
     $post = get_post($post_ID);
     if ( !wp_is_post_revision( $post_ID ) ) {
         $contenido = $post->post_content;
         $excerpt = substr($contenido,0,255);
         $permalink = get_permalink($post_ID);
         $authorURL = get_author_posts_url($post->post_author);
         $title = $post->post_title;
                 $result = $wpdb->get_results("SELECT * FROM wp_subscribe", ARRAY_A);
         $origen = "XXXX";
         $headers = "From: $origen\r\n";
         $headers .= "X-Mailer: PHP5\n";
         $headers .= 'MIME-Version: 1.0' . "\n";
         $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
         foreach($result as $row){
               // A lot of code for styling the mail-..
         mail($row['email'],$title,$contenido,$headers);
         }
     }
     return $post_ID;
}
add_action( 'publish_post', 'send_mails' );

有什么问题吗?或者我错过了一些功能来检查它是否是一个编辑过的帖子?

提前谢谢你

【问题讨论】:

  • 新帖子不会是修订版...
  • 我也是这样。但每次我更新网站上已经存在的帖子时,都会发送另一封邮件

标签: php wordpress email


【解决方案1】:

您可以在发送电子邮件时为帖子添加元值:

add_post_meta($post_id, 'email_sent', 'yes', true)

然后在发送电子邮件功能中检查这一点,这样它只会发送一次。

if( get_post_meta($post_id, 'email_sent', 'true') != 'yes' ) {
    //    send the email
   }

【讨论】:

  • 这就是我一直在寻找的答案。谢谢!
  • 没有 $post->post_status == 'published' 在草稿保存时不会发送邮件
【解决方案2】:

save_post 是每当创建或更新帖子或页面时触发的操作,可以来自导入、帖子/页面编辑表单、xmlrpc 或通过电子邮件发布。帖子的数据存储在 $_POST、$_GET 或全局 $post_data 中,具体取决于帖子的编辑方式。例如,快速编辑使用 $_POST。

add_action('save_post', 'function');

http://codex.wordpress.org/Plugin_API/Action_Reference/save_post

【讨论】:

    【解决方案3】:

    为新人更新答案。

    使用 save_post,它来自 WordPress 3.3,具有第三个参数“update”。所以例子是:

    function save_func($ID, $post,$update) {
    
       if($update == false) {
         // do something if its first time publish
       } else {
         // Do something if its update
       }
    }
    
    add_action( 'save_post', 'save_func', 10, 3 );
    

    就是这样。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-03-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多