8 公開通知

自作プラグイン

 投稿や固定ページが公開されると、サイト管理者にメールで通知します。
 通知するのは、状態が「auto-draft」や「下書き」からこれら以外に変化したときです。

 自作のフック'hk_func_insert_post_data'と'hk_func_save_post'に関しては“投稿作成のカスタマイズ(3)”をご覧ください。
 下記関数に関しては“共通関数”をご覧ください。
  hk_func_site_name
  hk_func_get_local_time

コード

// ================================
// 公開通知
// ================================
$hk_notice_publish_old_post_status = '';

// --------------------------------
// 更新前の post_status 取得
// --------------------------------
function hk_notice_publish_insert_post_data( $data, $post_id, $old_post ) {
    global $hk_notice_publish_old_post_status;
    $post_type = $data['post_type'];
    if (( $post_type != 'post' ) && ( $post_type != 'page' ))    return $data;
    $hk_notice_publish_old_post_status = $old_post->post_status;
    return $data;
}
add_filter( 'hk_func_insert_post_data', 'hk_notice_publish_insert_post_data', 20, 3 );

// --------------------------------
// 公開通知送信
// --------------------------------
function hk_notice_publish_save_post( $post_id, $post ) {
    global $hk_notice_publish_old_post_status;
    $post_type = $post->post_type;
    if (( $post_type != 'post' ) && ( $post_type != 'page' ))    return;
    // 新post_status:auto-draft,下書き を除外
    $post_status = $post->post_status;
    if (( $post_status == 'auto-draft' ) || ( $post_status == 'draft' ))    return;
    // 旧post_status:auto-draft,下書き 以外を除外
    if (( $hk_notice_publish_old_post_status != 'auto-draft' )
     && ( $hk_notice_publish_old_post_status != 'draft' ))       return;
    // サイト名
    $site_name = hk_func_site_name();
    // タイトル
    $post_title = $post->post_title;
    // リンク
    $post_link = get_permalink( $post_id );
    // カテゴリー名称
    if ( $post_type == 'post' ) {
        $categories = get_the_category( $post_id );
        $category   = $categories[0];
        $cat_name   = $category->name;
    }
    else
        $cat_name = '';
    // 作成者
    $author_id   = $post->post_author;
    $author_info = get_userdata( $author_id );
    $author_name = $author_info->last_name.' '.$author_info->first_name;
    // 現在時刻
    $date = hk_func_get_local_time();
    // メールタイトル
    if      ( $post_status == 'pending' )
        $title = 'ホームページ 投稿承認待ち';
    else if ( $post_status == 'future' )
        $title = 'ホームページ 投稿公開予約';
    else if ( $post_status == 'private' )
        $title = 'ホームページ 非公開投稿';
    else
        $title = 'ホームページ 新しい投稿が公開';
    // メールアドレス
    $admin_email = get_bloginfo('admin_email');
    $to   = $admin_email;
    $from = $admin_email;
    // HEADER
    $header = 'From: '.$from;
    // メール本文
    if      ( $post_status == 'pending' )
        $body  = "ホームページに新しい投稿が承認待ちです。\n\n";
    else if ( $post_status == 'future' )
        $body  = "ホームページに投稿公開予約がありました。\n\n";
    else if ( $post_status == 'private' )
        $body  = "ホームページに非公開投稿がありました。\n\n";
    else
        $body  = "ホームページに新しい投稿が公開されました。\n\n";
    $body .= "【ホームページ名】\n  ".$site_name."\n";
    $body .= "【作成者】\n  ".$author_name."\n";
    $body .= "【タイトル】\n  ".$post_title."\n";
    $body .= "【掲載ページ】\n  ".$post_link."\n";
    if ( $cat_name )
        $body .= "【カテゴリー】\n  ".$cat_name."\n";
    $body .= "【作成日時】\n  ".$date."\n";
    $body .= "\nデバッグ用データ\n";
    $body .= "【更新前post_status】\n  ".$hk_notice_publish_old_post_status."\n";
    $body .= "【 新 post_status】\n  ".$post_status."\n";
    // 送信
    mb_language('ja');
    mb_send_mail( $to , $title , $body , $header );
    return;
}
add_action( 'hk_func_save_post', 'hk_notice_publish_save_post', 10, 2 );

 このプログラムをお使いになる場合は、お使いになる方の自己責任でお願いします。

関連

更新日:2016/03/25
掲載日:2016/01/25