LoginSignup
23
24

More than 3 years have passed since last update.

WordPress初心者開発者向け フィルターフックとアクションフック入門

Last updated at Posted at 2019-12-19

アクションフック


アクションフック

do_action() の記述がある所に処理が追加できる

do_action( 'フック名' );

アクションフックポイントの例

header.php

<?php wp_head(); ?>

wp_head() の中身

function wp_head() {
    /**
     * Prints scripts or data in the head tag on the front end.
     *
     * @since 1.5.0
     */
    do_action( 'wp_head' );
}

とりあえず追加してみる

add_action('wp_head', 'example01');
function example01(){
    echo '@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@';
}

<head>タグに書いてある <?php wp_head(); ?> のタイミングで実行されてます。


基本の書式

add_action( '★フック名★', '■実行する関数名■' );
function ■実行する関数名■(){
    ●実行する処理内容●;
}

利用例

add_action('wp_head', 'example02');
function example02(){
    echo '<!-- [ OGタイトル ] -->'."\n";
    echo '<meta property="og:title" content="Lightning × ExUnit 日本語デモ" />'."\n";
    echo '<!-- [ /OGタイトル ] -->'."\n";
}

どういう場面で使うの?

  • テーマファイルなどは直接変更できない時にプラグインなど外部から処理を追加する

子テーマ直書きじゃいかんの?

フックで対応できるならフックで対応した方が影響範囲が少なくてすむ

例えばheader.phpの場合、たった一箇所の為に子テーマ化してしまうと、
親テーマでheader.phpのバージョンアップがあった場合に困る。

良くない例
<?php wp_head();?>
<meta property="og:title" content="Lightning × ExUnit 日本語デモ" />

アクションフックは外部から外せる

remove_action('wp_head', 'example01');

アクションフックの優先順位

3つ目の引数が優先順位になる。未記入の場合は10で処理されます。

下記のように 100 で設定すると、出力される位置がかなり後ろになるのが確認できると思います。

add_action('wp_head', 'example01', 100);
function example01(){
    echo '@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@';
}

優先順位の指定がある場合は、removeする時も優先順位を指定しないと効かないので注意

remove_action('wp_head', 'example01', 100);

アクションフックってどこにあんの?

「アクションフック 一覧」でGoogle先生に聞いてみる。


テーマやプラグイン内で do_action( で検索する

Lightning はテーマファイル内に do_action() がいろいろ置かれているので、
必要に応じてHTMLを挿入できます。

add_action('lightning_header_after', 'my_lightning_header_after');
function my_lightning_header_after(){
    echo '<div class="text-center">キャンペーンリンク</div>';
}


フィルターフック


フィルターフック

もともと出力したりするはずの値を改変する事ができる


とりあえず改変してみる

<?php the_content();?> で表示される内容を改変してみましょう

add_filter( 'the_content', 'filter_example01' );
function filter_example01( $content ){
    // もともとあった値 $content の前に 特定の文字列を追加
    $content = '<div>うえーい</div>' . $content;
    // 改変した値を返す(必須)
    return $content;
}

基本の書式

add_filter( '★フック名★', '■実行する関数名■' );
function ■実行する関数名■( ▲処理する変数名▲ ){
    ●実行する処理内容●;
    return ▲処理する変数名▲;
}

アクションフックと違って、もともと存在する値を改変するので、処理する変数が存在する


フィルターフック(改変)ができる場所はどうなってるの?

apply_filters() 関数が使ってある箇所

function the_content( $more_link_text = null, $strip_teaser = false ) {
    // 略 //
    $content = apply_filters( 'the_content', $content );
    // 略 //
    echo $content;
}
▲処理する変数名▲ = apply_filters('★フィルターフック名★', ▲処理する変数名▲ );

というような書式で書かれている事が多いので、
WordPress本体やテーマ・プラグインのファイル内に上記の記述があります。


フィルターフック(改変)ができる場所はどうやって調べるの?

→ 「apply_filters(」 でWordPressのフォルダやテーマ・プラグインのフォルダを検索する

・・・沢山ありすぎでよくわからないと思います。

→ 改変したい関数の中身を除きに行くのが一番かと...。


フィルターフックでTweetボタンを追加してみる

// Tweerボタン
add_filter( 'the_content', 'my_tweet_button_test' );
function my_tweet_button_test( $content ){
    $content .= '<a href="https://twitter.com/share" class="twitter-share-button" data-url="'.get_permalink().'" data-lang="ja" data-via="kurudrive">ツイート</a>
<script>!function(d,s,id){var js,fjs=d.getElementsByTagName(s)[0];if(!d.getElementById(id)){js=d.createElement(s);js.id=id;js.src="//platform.twitter.com/widgets.js";fjs.parentNode.insertBefore(js,fjs);}}(document,"script","twitter-wjs");</script>';
    return $content;
}

Lightning でのフィルター利用例

/*-------------------------------------------*/
/* footer カラム数を4カラムに変更
/*-------------------------------------------*/
add_filter( 'lightning_footer_widget_area_count', 'my_footer_change' );
function my_footer_change($footer_widget_area_count){
    $footer_widget_area_count = 4;
    return $footer_widget_area_count;
}

/*-------------------------------------------*/
/* ページ上部の表示タイトル名を変更
/*-------------------------------------------*/
add_filter( 'lightning_pageTitCustom','my_page_title_custom' );
function my_page_title_custom($pageTitle){
    // 変更する条件を指定
    if ( is_page() ){ // 固定ページの場合
        // 変更後の名前
        $pageTitle = '変更します';
    }
    return $pageTitle;
}

フィルターやアクションが効かない時!!


ケース1 実行タイミング

子テーマのfunctions.php

add_filter( 'the_content', 'filter_example01' );
function filter_example01( $content ){
    $content = '<div>うえーい</div>' . $content;
    return $content;
}

↑ うえーい が表示される


子テーマのfunctions.php の下に下記を追加

remove_filter( 'the_content', 'filter_example01' );

↑ 消えるのが確できます。ところが...


/wp-content/plugins/sample.php を作成 し以下の内容で有効化

<?php
/*
Plugin Name: hook sample
*/

remove_filter( 'the_content', 'filter_example01' );

→ 「うえーい」 は消えない!


子テーマのfunctions.phpよりもプラグインのファイルの方が先に読み込まれるため、

  1. remove_filter() を実行
  2. add_filter() を実行

という状態なので効かない

どうすんの?


add_action() を使って実行タイミングをずらす

add_action( 'after_setup_theme', 'remove_filter_example01' );
function remove_filter_example01(){
    remove_filter( 'the_content', 'filter_example01' );
}

テーマ関連で指定されているものであればとりあえず after_setup_theme あたりで試す。
ダメなら アクションフック一覧 を見て順番に変更してみる


WordPressでremove_actionが効かない時の対処メモ
https://qiita.com/kurudrive/items/729c2ce8d7ac5026759e

class内のフックを外したりする時のメモ
https://qiita.com/kurudrive/items/2d686df54065e247c6a5

23
24
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
23
24