Title: 高度な話題
Author: Aki Hamano
Published: 2025年5月9日
Last modified: 2025年5月11日

---

# 高度な話題

## この投稿内

 * [アクションおよびフィルターの削除](https://ja.wordpress.org/team/handbook/plugin-development/hooks/advanced-topics/?output_format=md#%e3%82%a2%e3%82%af%e3%82%b7%e3%83%a7%e3%83%b3%e3%81%8a%e3%82%88%e3%81%b3%e3%83%95%e3%82%a3%e3%83%ab%e3%82%bf%e3%83%bc%e3%81%ae%e5%89%8a%e9%99%a4)
 * [例](https://ja.wordpress.org/team/handbook/plugin-development/hooks/advanced-topics/?output_format=md#%e4%be%8b)
 * [すべてのコールバックの削除](https://ja.wordpress.org/team/handbook/plugin-development/hooks/advanced-topics/?output_format=md#%e3%81%99%e3%81%b9%e3%81%a6%e3%81%ae%e3%82%b3%e3%83%bc%e3%83%ab%e3%83%90%e3%83%83%e3%82%af%e3%81%ae%e5%89%8a%e9%99%a4)
 * [現在のフックの特定](https://ja.wordpress.org/team/handbook/plugin-development/hooks/advanced-topics/?output_format=md#%e7%8f%be%e5%9c%a8%e3%81%ae%e3%83%95%e3%83%83%e3%82%af%e3%81%ae%e7%89%b9%e5%ae%9a)
 * [フックの作動回数の確認](https://ja.wordpress.org/team/handbook/plugin-development/hooks/advanced-topics/?output_format=md#%e3%83%95%e3%83%83%e3%82%af%e3%81%ae%e4%bd%9c%e5%8b%95%e5%9b%9e%e6%95%b0%e3%81%ae%e7%a2%ba%e8%aa%8d)
 * [「all」フックを使ったデバッグ](https://ja.wordpress.org/team/handbook/plugin-development/hooks/advanced-topics/?output_format=md#%e3%80%8call%e3%80%8d%e3%83%95%e3%83%83%e3%82%af%e3%82%92%e4%bd%bf%e3%81%a3%e3%81%9f%e3%83%87%e3%83%90%e3%83%83%e3%82%b0)

[↑ ページの先頭へ戻る](https://ja.wordpress.org/team/handbook/plugin-development/hooks/advanced-topics/?output_format=md#wp--skip-link--target)

## 󠀁[アクションおよびフィルターの削除](https://ja.wordpress.org/team/handbook/plugin-development/hooks/advanced-topics/?output_format=md#%e3%82%a2%e3%82%af%e3%82%b7%e3%83%a7%e3%83%b3%e3%81%8a%e3%82%88%e3%81%b3%e3%83%95%e3%82%a3%e3%83%ab%e3%82%bf%e3%83%bc%e3%81%ae%e5%89%8a%e9%99%a4)󠁿

他のプラグインやテーマ、あるいは WordPress コアが登録したフックからコールバック関数
を削除したいことがあります。

フックからコールバック関数を削除する場合は、コールバック関数がアクションとして追加
されたのかフィルターとして追加されたのかによって、`remove_action()` か `remove_filter()`
を呼び出す必要があります。

`remove_action()` / `remove_filter()` に渡されるパラメータは、それを登録した `add_action()`/`
add_filter()` に渡されるパラメータと同一でなければ、削除がうまく働きません。

コールバック関数を正常に削除する場合は、コールバック関数が登録された後に削除する
必要があります。実行順序は重要です。

## 󠀁[例](https://ja.wordpress.org/team/handbook/plugin-development/hooks/advanced-topics/?output_format=md#%e4%be%8b)󠁿

たとえば、不要な機能を削除して大規模テーマのパフォーマンスを向上させたいとしましょう。

`functions.php` を見て、テーマのコードを分析してみましょう。

    ```notranslate
    function wporg_setup_slider() {
      // ...
    }
    add_action( 'template_redirect', 'wporg_setup_slider', 9 );
    ```

`wporg_setup_slider` 関数は、必要のないスライダーを追加しており、おそらく巨大な 
CSS ファイルをロードし、その後に、1MB サイズのカスタムライブラリを使用する JavaScript
の初期化ファイルをロードしています。それを取り除くことができます。

`wporg_setup_slider` コールバック関数が登録された (`functions.php` が実行された)
後に WordPress にフックしたいので、最善の方法は [`after_setup_theme`](https://developer.wordpress.org/reference/hooks/after_setup_theme/)
フックでしょう。

    ```notranslate
    function wporg_disable_slider() {
      // Make sure all parameters match the add_action() call exactly.
      remove_action( 'template_redirect', 'wporg_setup_slider', 9 );
    }
    // Make sure we call remove_action() after add_action() has been called.
    add_action( 'after_setup_theme', 'wporg_disable_slider' );
    ```

## 󠀁[すべてのコールバックの削除](https://ja.wordpress.org/team/handbook/plugin-development/hooks/advanced-topics/?output_format=md#%e3%81%99%e3%81%b9%e3%81%a6%e3%81%ae%e3%82%b3%e3%83%bc%e3%83%ab%e3%83%90%e3%83%83%e3%82%af%e3%81%ae%e5%89%8a%e9%99%a4)󠁿

`remove_all_actions()` / `remove_all_filters()` を使えば、フックに関連するコール
バック関数をすべて削除できます。

## 󠀁[現在のフックの特定](https://ja.wordpress.org/team/handbook/plugin-development/hooks/advanced-topics/?output_format=md#%e7%8f%be%e5%9c%a8%e3%81%ae%e3%83%95%e3%83%83%e3%82%af%e3%81%ae%e7%89%b9%e5%ae%9a)󠁿

複数のフックに対してアクションやフィルターを作動させたいが、現在どのフックが呼び出し
ているかによって動作を変えたいことがあります。

`current_action()` / `current_filter()` を使用して、現在のアクション/フィルターを
特定できます。

    ```notranslate
    function wporg_modify_content( $content ) {
      switch ( current_filter() ) {
        case 'the_content':
          // Do something.
          break;
        case 'the_excerpt':
          // Do something.
          break;
      }
      return $content;
    }

    add_filter( 'the_content', 'wporg_modify_content' );
    add_filter( 'the_excerpt', 'wporg_modify_content' );
    ```

## 󠀁[フックの作動回数の確認](https://ja.wordpress.org/team/handbook/plugin-development/hooks/advanced-topics/?output_format=md#%e3%83%95%e3%83%83%e3%82%af%e3%81%ae%e4%bd%9c%e5%8b%95%e5%9b%9e%e6%95%b0%e3%81%ae%e7%a2%ba%e8%aa%8d)󠁿

フックの中には実行の過程で何度も呼び出されるものもありますが、コールバック関数は
一度だけ実行させたい場合もあるでしょう。

この場合、フックが何回作動したかを [`did_action()`](https://developer.wordpress.org/reference/functions/did_action/)
で確認できます。

    ```notranslate
    function wporg_custom() {
      // If save_post has been run more than once, skip the rest of the code.
      if ( did_action( 'save_post' ) !== 1 ) {
        return;
      }
      // ...
    }
    add_action( 'save_post', 'wporg_custom' );
    ```

## 󠀁[「all」フックを使ったデバッグ](https://ja.wordpress.org/team/handbook/plugin-development/hooks/advanced-topics/?output_format=md#%e3%80%8call%e3%80%8d%e3%83%95%e3%83%83%e3%82%af%e3%82%92%e4%bd%bf%e3%81%a3%e3%81%9f%e3%83%87%e3%83%90%e3%83%83%e3%82%b0)󠁿

すべてのフックに対してコールバック関数を起動させたい場合は、`all` フックに登録し
ます。これはデバッグの場面で、ある特殊なイベントが発生したときや、ページがクラッシュ
したときを確定するのに役立つことがあります。

    ```notranslate
    function wporg_debug() {
      echo '<p>' . current_action() . '</p>';
    }
    add_action( 'all', 'wporg_debug' );
    ```

[原文](https://developer.wordpress.org/plugins/hooks/advanced-topics/) / [日本語訳](https://github.com/jawordpressorg/developer-plugins-handbook/blob/main/hooks/advanced-topics/index.md)

公開日

2025年5月9日

最終更新

2025年5月11日

[  前へ: カスタム・フック](https://ja.wordpress.org/team/handbook/plugin-development/hooks/custom-hooks/)

[  次へ: プライバシー](https://ja.wordpress.org/team/handbook/plugin-development/privacy/)