[Osaka/Yokohama] Looking for infrastructure/server side engineers!

[Osaka/Yokohama] Looking for infrastructure/server side engineers!

[Deployed by over 500 companies] AWS construction, operation, maintenance, and monitoring services

[Deployed by over 500 companies] AWS construction, operation, maintenance, and monitoring services

[Successor to CentOS] AlmaLinux OS server construction/migration service

[Successor to CentOS] AlmaLinux OS server construction/migration service

[For WordPress only] Cloud server “Web Speed”

[For WordPress only] Cloud server “Web Speed”

[Cheap] Website security automatic diagnosis “Quick Scanner”

[Cheap] Website security automatic diagnosis “Quick Scanner”

[Reservation system development] EDISONE customization development service

[Reservation system development] EDISONE customization development service

[Registration of 100 URLs is 0 yen] Website monitoring service “Appmill”

[Registration of 100 URLs is 0 yen] Website monitoring service “Appmill”

[Compatible with over 200 countries] Global eSIM “Beyond SIM”

[Compatible with over 200 countries] Global eSIM “Beyond SIM”

[If you are traveling, business trip, or stationed in China] Chinese SIM service “Choco SIM”

[If you are traveling, business trip, or stationed in China] Chinese SIM service “Choco SIM”

[Global exclusive service] Beyond's MSP in North America and China

[Global exclusive service] Beyond's MSP in North America and China

[YouTube] Beyond official channel “Biyomaru Channel”

[YouTube] Beyond official channel “Biyomaru Channel”

勢いでWordPressのちょっとしたカスタマイズをした話

こんにちは。
開発チームのワイルド担当、まんだいです。

このビヨンドブログでは、技術面や文法でおかしなところがないかチェックをするために、書いた人とは別の人がダブルチェックをするという工程を経て、公開されるようになっています。
今までは、チャットワーク上で「この記事書いたから見て~」みたいなやり取りをしていたのですが、いちいち書いた後にチャットワークで報告するの、面倒だよなぁと思ったので、レビュー待ちになったらチェックをお願いするスクリプトを勢いで書いてみました。

 

何をどうしたのか

ブログの記事を書き終わった後、「下書き」の状態から、「レビュー待ち」という状態にするのをフックにして、Chatwork APIを通じて確認依頼を投稿するという機能を付けました。

プログラム的にやっていることは、以下の通りです。

  • ブログ記事の遷移状態のチェック
  • 記事の情報、書いた人の情報を収集
  • Chatwork APIにPOST

これだけです。

で、これが完成したソースです。

/**
 * レビュー待ちのものをチャットワークに投稿
 */
function announce_blog_status_in_review($new_status, $old_status, $post){
    if ($old_status != 'pending' && $new_status == 'pending'){
        $cw_room_id = 'XXXXXXXX';
        $cw_api_key = 'XXXXXXXXXXXXXXXXXXXXXXXXXXX';
        $cw_to = 'xxxxxxxx';

        $cw_target_url = 'https://api.chatwork.com/v2/rooms/'. $cw_room_id. '/messages';

        $blog_title = wp_specialchars_decode($post->post_title, ENT_QUOTES);
        $blog_author = wp_specialchars_decode(get_the_author_meta('last_name', $post->post_author), ENT_QUOTES);
        $message = "[info][title]レビューはいりました[/title]\n";
        $message.= "[To:$cw_to] ブログおじさんさん\n";
        $message.= $blog_author. "さんがブログをレビューを待っています!\n";
        $message.= "タイトル : ". $blog_title. "\n";
        $message.= "URL : $post->guid[/info]";

        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $cw_target_url);
        curl_setopt($ch, CURLOPT_HTTPHEADER, array('X-ChatWorkToken: '. $cw_api_key));
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_POST, true);
        curl_setopt($ch, CURLOPT_POSTFIELDS, array('body' => $message));
        curl_exec($ch);
    }
}
add_action('transition_post_status', 'announce_blog_status_in_review', 10, 3);

 
これを実現するためには、wordpressが提供する関数について、少し知っている必要があったので、それについて触れたいと思います。

 

get_the_author_meta()関数

この関数から、書いた人の情報を取得します。
関数の第3引数になっている$postの中に、author_idは入っていますが、肝心の名前は分かりません。

IDを見ても、それが誰だか分かりませんので、author_idから各種情報を取得するための関数がこちらになります。

関数の第1引数に欲しい情報のキーをセットします。
たくさんあるのですが、全てのキーをまとめたサイトを見つけたので、リンクを貼っておきます。

WordPress に登録されているユーザーの様々なデータを取り出す方法

 

add_action()関数

せっかく関数を作っても、それがいつ実行されるか、フックを設定しないと実行されませんよね。
そのフックに関数を登録する関数がこのadd_action()関数になります。

対になる第1引数が同じ値が設定された、do_action()関数が実行されたタイミングで、add_action()関数によって登録された関数は実行されます。

と言っても、do_action()関数は色んな所に設置されていて、いろんなタイミングで処理をフックできるようになっています。

今回フックした、「transition_post_status」というアクションは、記事のステータスが変更された場合に呼び出されます。
ただ、記事のステータスは実はたくさんあって、変更前と変更後のアクションの組み合わせがわかるので、下書きからレビュー待ちになった場合のみ、処理を行うようにしています。

 

まとめ

wordpressはどんどん機能が追加されて規模が大きくなっているので、なかなかとっつきにくい感もありますが、調べればすぐに何らかの情報は出てくるので、気負わずカスタマイズできるなと思います。
そして意外にカスタマイズが簡単!

沢山の人に使われているだけありますね。

いい感じにパッとできて気分が良かったので、この記事も勢いで書いたというオチでございました。

 
以上です。

この記事がお役に立てば【 いいね 】のご協力をお願いいたします!
0
読み込み中...
0 票, 平均: 0.00 / 10
696
X facebook はてなブックマーク pocket
[2024.6.30 CentOS support ended] CentOS server migration solution

[2024.6.30 CentOS support ended] CentOS server migration solution

[2025.6.30 Amazon Linux 2 support ended] Amazon Linux server migration solution

[2025.6.30 Amazon Linux 2 support ended] Amazon Linux server migration solution

[Osaka/Yokohama] Actively recruiting infrastructure engineers and server side engineers!

[Osaka/Yokohama] Actively recruiting infrastructure engineers and server side engineers!

The person who wrote this article

About the author

Yoichi Bandai

My main job is developing web APIs for social games, but I'm also fortunate to be able to do a lot of other work, including marketing.
Furthermore, my portrait rights in Beyond are treated as CC0 by him.