2016-10-25 5 views
0

投稿タイプが保留になったときに電子メールを送信しています。電子メールを2回送信する(Wordpress)

すべて正常に動作しますが、電子メールは2回送信されます。誰かが助けることができる

function pendingPost($id) 
{ 
$type = get_post_type($id); 
$post_user = wp_get_current_user($id); 
$edit_link = get_edit_post_link($id); 
$to = "[email protected]"; 
$subject = "New Entry needs to be approved"; 
    $message = " 
      <html> 
      <head> 
      <title>Pending notification</title> 
      </head> 
      <body> 
      <p>Hi,</p> 
      <p>A new entry has been submited.</p> 
      <p>Title : ".get_the_title($id)."</p> 
      <p>Type : ".substr($type, 0, -1)."</p> 
      <p>Author : ".$post_user->display_name."</p> 
      <br /> 
      <p>Click <a href='".$edit_link."'>here</a> to approve it.</p> 
      </body> 
      </html> 
      "; 

    $headers = array('Content-Type: text/html; charset=UTF-8'); 

    $mail = wp_mail($to, $subject, $message,$headers); 

} 

add_action('pending_posttype', 'pendingPost', 10, 1); 

希望:

は、ここに私のコードです。

ありがとうございます。

答えて

1

アクションフックpending_posttypeがおそらく2回発生します。 did_action()を使用すると、その操作が既に開始されているかどうかを確認できます。

function pendingPost($id) { 

    if (did_action('pending_posttype') !== 1) return; 

    $type = get_post_type($id); 
    $post_user = wp_get_current_user($id); 
    $edit_link = get_edit_post_link($id); 
    $to = "[email protected]"; 
    $subject = "New Entry needs to be approved"; 
    $message = " 
      <html> 
      <head> 
      <title>Pending notification</title> 
      </head> 
      <body> 
      <p>Hi,</p> 
      <p>A new entry has been submited.</p> 
      <p>Title : ".get_the_title($id)."</p> 
      <p>Type : ".substr($type, 0, -1)."</p> 
      <p>Author : ".$post_user->display_name."</p> 
      <br /> 
      <p>Click <a href='".$edit_link."'>here</a> to approve it.</p> 
      </body> 
      </html> 
      "; 

    $headers = array('Content-Type: text/html; charset=UTF-8'); 

    $mail = wp_mail($to, $subject, $message,$headers); 

} 

add_action('pending_posttype', 'pendingPost', 10, 1); 
+0

前に呼ばれた 'add_action( 'save_post'、 'published_to_pending');がありました。私はすべてのコードを一つの関数の中に入れなければならなかった。 – devofash

関連する問題