- 赤色のリンクは、まだ日本語Codexに存在しないページ・画像です。英語版と併せてご覧ください。(詳細)
「投稿ステータスの遷移」の版間の差分
1行目: | 1行目: | ||
− | |||
− | |||
=== 概要 === | === 概要 === | ||
25行目: | 23行目: | ||
* <tt>trash</tt> – 「ゴミ箱」にある投稿([[Version 2.9|バージョン 2.9]] で追加)。 | * <tt>trash</tt> – 「ゴミ箱」にある投稿([[Version 2.9|バージョン 2.9]] で追加)。 | ||
− | < | + | <p class="information">'''参考:''' 投稿ステータスの詳しい説明は [[投稿ステータス]] を見てください。カスタム投稿ステータスを追加することもできます。</p> |
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | '' | + | |
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
<div id="transition_post_status_Hook"> | <div id="transition_post_status_Hook"> | ||
49行目: | 29行目: | ||
</div> | </div> | ||
− | <tt>[[プラグイン API/アクションフック一覧/transition_post_status|transition_post_status]]</tt>/[[:en:Plugin_API/Action_Reference/transition_post_status|en]] は、投稿ステータスが変わるとき毎回呼び出される包括的なアクションです。<tt>transition_post_status</tt> | + | <tt>[[プラグイン API/アクションフック一覧/transition_post_status|transition_post_status]]</tt>/[[:en:Plugin_API/Action_Reference/transition_post_status|en]] は、投稿ステータスが変わるとき毎回呼び出される包括的なアクションです。<tt>transition_post_status</tt> にフックされたコールバック関数が [[関数リファレンス/do_action|do_action()]] を使って呼び出されるとき、3 つの引数が渡されます: <tt>$new_status</tt>, <tt>$old_status</tt> それと <tt>$post</tt> オブジェクトです。従ってコールバック関数はこれらを利用できます。<tt>[[関数リファレンス/add_action|add_action()]]</tt> を呼び出すとき、アクションの優先度を 0 から 20 で指定し(デフォルトは 10)、<tt>do_action()</tt> がコールバック関数へ渡す引数の個数を指定しなければなりません。 |
<pre>function on_all_status_transitions( $new_status, $old_status, $post ) { | <pre>function on_all_status_transitions( $new_status, $old_status, $post ) { | ||
65行目: | 45行目: | ||
add_action( 'transition_post_status', 'post_unpublished', 10, 3 );</pre> | add_action( 'transition_post_status', 'post_unpublished', 10, 3 );</pre> | ||
− | アクションフック <tt>transition_post_status</tt> と、関数 <tt>[[関数リファレンス/wp_transition_post_status|wp_transition_post_status()]]</tt>/[[:en:Function_Reference/wp_transition_post_status|en]] またはプライベート関数 <tt>[[関数リファレンス/_transition_post_status|_transition_post_status()]]</tt>/[[:en:Function_Reference/_transition_post_status|en]] を混同しないでください。関数 <tt>wp_transition_post_status()</tt> は投稿ステータスの変化時にアクションを呼び出すために使用され、{{Trac|wp-includes/post.php}} にあります。この関数は、プラグインが直接データベースを変更したい場合に役立ちます(その場合、普通の投稿ステータス変化に関するアクションフックをバイパスします)。もし投稿ステータスの変化時にアクションを実行するのでなく、投稿のステータスを変えたい場合は、[[関数リファレンス/wp_update_post|wp_update_post()]] または [[関数リファレンス/wp_publish_post|wp_publish_post()]] を使用してください。 | + | '''注意:''' アクションフック <tt>transition_post_status</tt> と、関数 <tt>[[関数リファレンス/wp_transition_post_status|wp_transition_post_status()]]</tt>/[[:en:Function_Reference/wp_transition_post_status|en]] またはプライベート関数 <tt>[[関数リファレンス/_transition_post_status|_transition_post_status()]]</tt>/[[:en:Function_Reference/_transition_post_status|en]] を混同しないでください。関数 <tt>wp_transition_post_status()</tt> は投稿ステータスの変化時にアクションを呼び出すために使用され、{{Trac|wp-includes/post.php}} にあります。この関数は、プラグインが直接データベースを変更したい場合に役立ちます(その場合、普通の投稿ステータス変化に関するアクションフックをバイパスします)。もし投稿ステータスの変化時にアクションを実行するのでなく、投稿のステータスを変えたい場合は、[[関数リファレンス/wp_update_post|wp_update_post()]] または [[関数リファレンス/wp_publish_post|wp_publish_post()]] を使用してください。 |
<div id=".7Bold_status.7D_to_.7Bnew_status.7D_Hook"> | <div id=".7Bold_status.7D_to_.7Bnew_status.7D_Hook"> | ||
105行目: | 85行目: | ||
</div> | </div> | ||
− | <tt>private_to_published</tt> | + | * <tt>private_to_published</tt> は非推奨となりました。必要なら <tt>private_to_publish</tt> が使えます。 |
− | + | * <tt>publish_future_post</tt> は非推奨となりました。必要なら <tt>future_to_publish</tt> が使えます。 | |
− | <tt>publish_future_post</tt> | + | |
<div id="Related"> | <div id="Related"> | ||
− | == | + | == 関連項目 == |
</div> | </div> | ||
121行目: | 100行目: | ||
* [[関数リファレンス/wp_update_post|wp_update_post()]] | * [[関数リファレンス/wp_update_post|wp_update_post()]] | ||
* [[関数リファレンス/wp_publish_post|wp_publish_post()]] | * [[関数リファレンス/wp_publish_post|wp_publish_post()]] | ||
+ | * [[投稿ステータス]] | ||
<div id="Source_File"> | <div id="Source_File"> | ||
128行目: | 108行目: | ||
関数 <tt>wp_transition_post_status()</tt> は {{Trac|wp-includes/post.php}} をご覧ください。 | 関数 <tt>wp_transition_post_status()</tt> は {{Trac|wp-includes/post.php}} をご覧ください。 | ||
− | {{原文|Post_Status_Transitions| | + | {{原文|Post_Status_Transitions|151652}} <!-- 12:51, 16 May 2015 Miccweb 版 --> |
[[Category:上級トピック]] | [[Category:上級トピック]] |
2015年7月5日 (日) 08:41時点における最新版
目次
概要
WordPress 2.3 から、プラグインが投稿ステータス変更ワークフローにフックをかけられるよう、幾つかの新しいアクションが追加されました。以前、WordPress はアクション private_to_published
を持っており、これは投稿が非公開(private)から公開済み(published)になるときだけ呼び出されました。いま、WordPress は有り得るすべての遷移に関してアクションを持っています。
投稿ステータスの遷移について三種類のアクションフックがあります:
- transition_post_status/en
- {old_status}_to_{new_status} – old_status は以前の、new_status は新しいステータス。
- {status}_{post_type} – status は新しいステータス、post_type は投稿タイプ。
三種類のアクションは何れも wp_transition_post_status()/en から呼び出されます。この関数は wp-includes/post.php
にあります。
投稿ステータスには下記の種類があります:
- new - 以前のステータスが存在しない状態。このステータスを含むフックは、"save_post" が実行されるとき毎回実行されます。
- publish – 投稿や固定ページが「公開済」。
- pending – 「承認待ち」の投稿。
- draft – 「下書き」の投稿。
- auto-draft – 「自動保存」。作成されたばかりの投稿で、コンテンツがない。
- future – 「予約済」。未来に公開を予定されている投稿。
- private – 「非公開」。ログインしていないユーザーには表示されない。
- inherit – 「継承」。リビジョンまたは添付ファイル(get_children() を参照のこと)。
- trash – 「ゴミ箱」にある投稿(バージョン 2.9 で追加)。
参考: 投稿ステータスの詳しい説明は 投稿ステータス を見てください。カスタム投稿ステータスを追加することもできます。
transition_post_status フック
transition_post_status/en は、投稿ステータスが変わるとき毎回呼び出される包括的なアクションです。transition_post_status にフックされたコールバック関数が do_action() を使って呼び出されるとき、3 つの引数が渡されます: $new_status, $old_status それと $post オブジェクトです。従ってコールバック関数はこれらを利用できます。add_action() を呼び出すとき、アクションの優先度を 0 から 20 で指定し(デフォルトは 10)、do_action() がコールバック関数へ渡す引数の個数を指定しなければなりません。
function on_all_status_transitions( $new_status, $old_status, $post ) { if ( $new_status != $old_status ) { // 投稿ステータスが任意に変わるとき実行する処理。 } } add_action( 'transition_post_status', 'on_all_status_transitions', 10, 3 );
function post_unpublished( $new_status, $old_status, $post ) { if ( $old_status == 'publish' && $new_status != 'publish' ) { // 投稿ステータスが公開済からそれ以外へ変化するとき実行する処理。 } } add_action( 'transition_post_status', 'post_unpublished', 10, 3 );
注意: アクションフック transition_post_status と、関数 wp_transition_post_status()/en またはプライベート関数 _transition_post_status()/en を混同しないでください。関数 wp_transition_post_status() は投稿ステータスの変化時にアクションを呼び出すために使用され、wp-includes/post.php
にあります。この関数は、プラグインが直接データベースを変更したい場合に役立ちます(その場合、普通の投稿ステータス変化に関するアクションフックをバイパスします)。もし投稿ステータスの変化時にアクションを実行するのでなく、投稿のステータスを変えたい場合は、wp_update_post() または wp_publish_post() を使用してください。
{old_status}_to_{new_status} フック
{old_status}_to_{new_status} アクションは、投稿ステータスが {old_status} から {new_status} へ変わるときに実行されます。このアクションには $post(投稿)オブジェクトが渡されます。add_action() を呼び出すとき、アクションの優先度を 0 から 20 で指定し(デフォルトは 10)、do_action() がコールバック関数へ渡す引数の個数を指定しなければなりません。
function on_publish_pending_post( $post ) { // 承認待ちの投稿が公開されるとき実行する処理。 } add_action( 'pending_to_publish', 'on_publish_pending_post', 10, 1 );
{status}_{post_type} フック
{status}_{post_type} アクションは、タイプが {post_type} の投稿が任意のステータスから {status} へ変化するときに実行されます。例えば、投稿が公開されると publish_post アクションが呼び出されます。このアクションには投稿 ID と $post(投稿)オブジェクトが渡されます。投稿タイプに指定できるデフォルトの値は post, page, attachment, revision, navigation の何れかです。さらにカスタム投稿タイプも使用できます。
function on_post_publish( $ID, $post ) { // 投稿が公開されるとき実行する処理。 } add_action( 'publish_post', 'on_post_publish', 10, 2 );
function on_post_scheduled( $ID, $post ) { // 投稿の公開が予約されるときに実行する処理。 } add_action( 'future_post', 'on_post_scheduled', 10, 2 );
非推奨のフック
- private_to_published は非推奨となりました。必要なら private_to_publish が使えます。
- publish_future_post は非推奨となりました。必要なら future_to_publish が使えます。
関連項目
- アクション transition_post_status/en
- wp_transition_post_status()/en
- アクション publish_post/en
- アクション publish_page/en
- 投稿タイプ
- add_action()
- wp_update_post()
- wp_publish_post()
- 投稿ステータス
ソースファイル
関数 wp_transition_post_status() は wp-includes/post.php
をご覧ください。