親テーマの必要な機能を子テーマで有効化する : WordPress

Pocket

親テーマのfunctions.phpで定義した処理で必要な処理だけを子テーマで有効化する。

  1. 親テーマのfunctions.phpで処理をクラス化する。
  2. 子テーマのfunctions.phpの中でafter_setup_themeを使い親テーマの必要な処理を有効化する。

functions.phpは子のfunctions.php ⇒ 親のfunctions.phpの順で読み込まれる[1]。after_setup_themeはコールバック関数の処理を親のfunctions.phpを読み込んだ後に実行する[2]

親テーマでクラス化した機能を子テーマのfunctions.phpでafter_setup_themeを使って有効化する簡単な例。

wp-content
    |
    |-- themes
          |-- parent
          |     |-- style.css
          |     |-- index.php
          |     |-- functions.php
          |
          |-- child
                |-- style.css
                |-- index.php
                |-- functions.php

親テーマのfunctions.php。

<?php                                                                                                                                                            // 子テーマで選択して利用する処理のクラス                                                                        
class MyBuiltIn {
    public static  function enableSidebar () {
        /** 
         * Widget
         */
        register_sidebar(
            array('name' => 'widget')
        );  
    }   
}
// ....................
// 親テーマの処理
// ....................
?>

子テーマのfunctions.php。after_setup_themeを使って利用する機能を有効化。


» WordPress, 親テーマfunctions.phpのaction, filterを子テーマでremove | beginsprite log

[1] functions.php

style.css と違い、functions.php は同名ファイルでオーバーライドできません。その代わり、親の functions.php に追加して読み込まれます。正確にいうと、親テーマの functions.php の直前に読み込まれます。したがって、もし親テーマの functions.php で favicon_link() という関数があるとき、子テーマのfunctions.php で同名の関数があれば、子テーマの関数が使用されます。

» 子テーマ – WordPress Codex 日本語版
[2]読み込み順を変更するわけではない。

コメント

No comments yet.

コメントの投稿

改行と段落タグは自動で挿入されます。
メールアドレスは表示されません。