WordPress functions.php
更新日:2019年02月19日
作成日:2019年02月02日
functions.php ファイル
テーマを作成する際に使用するファイルの1つ functions.php は、機能に関する設定(オプションの関数など)を記述するテーマファイルで、テーマと一緒に wp-content/themes のテーマ用ディレクトリに配置します。
場所:インストールディレクトリ / wp-content / themes / テーマフォルダ / functions.php
以下は、WordPress の公式テーマ「Twenty Nineteen」の例です。
functions.php はエディタでファイルを直接開いて編集することができます。
また、管理画面(「外観」→「テーマの編集」→「テーマのための関数」)からもアクセス及び編集することができます。
functions.php の特徴
functions.php は、以下のような特徴があります。
- テーマと一緒に wp-content/themes のテーマ用ディレクトリーへ配置します。
- 現在有効なテーマのディレクトリーにあるときのみ実行され、そのテーマに対してのみ機能します。
- ダッシュボード(編集画面のページなど)やテンプレート(公開ページ)の表示に先立って読み込まれて実行されます。
- PHP の関数や WordPress 組み込みの関数を呼び出したり、テーマで使う独自の関数を定義することができます。
- テーマのテンプレートより先に読み込まれるので、テーマの初期化の処理が行えます。
- WordPress の機能を変更するアクションやフィルターの定義を行うことができます。
参考サイト:
- Codex 日本語版:functions.php 概説
記述上の注意点
functions.php はテンプレート(公開ページのファイル)より先に読み込まれるので、以下のような直接何かを出力するような記述はしません。
//functions.php echo "Hello!";
上記のように記述してしまうと、HTML の <!DOCTYPE html> より前に "Hello!" が出力されてしまい、HTML の出力は以下のようになってしまいます。
Hello!<!DOCTYPE html> <html lang="ja"> <head>
ページに値を出力する場合は、関数を作成してその関数をページ(テンプレート)で実行します。
//functions.php function my_echo() { echo "Hello!"; }
//テンプレート(index.php など)で実行 <p><?php my_echo(); ?></p>
一度 PHP を終了させて出力
以下は少し特殊な記述例です。一度 PHP を終了させて出力していますが、実際に出力されるのは関数の実行時なので、この場合は問題ありません。
内容的には、add_action() を使って add_google_analytics() と言う関数を wp_head アクションにフックしています。
4行目で一度 PHP を終了させて、5行目~13行目をこの関数の実行時に出力させています。
add_action('wp_head','add_google_analytics'); function add_google_analytics() { if( !is_user_logged_in() ) { ?> <script> (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){ (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o), m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m) })(window,document,'script','//www.google-analytics.com/analytics.js','ga'); ga('create', 'UA-xxxxxxxxxxx', 'xxxxxx.com'); ga('require', 'displayfeatures'); ga('send', 'pageview'); </script> <?php } }
関連ページ:「一度 PHP を終了させて出力」
別ファイルの読み込み
functions.php に独自の関数や機能を色々と記述していくと、何がどこに書いてあるのかが判らず管理が大変になってくることがあります。
そのような場合は、関数やファイルの読み込みなど機能ごとにファイルを分離してそれぞれを functions.php に読み込むようにすると管理しやすくなります。
以下はテーマフォルダ内に「inc」というフォルダを作成して、その中に機能別のファイル(以下の例では scripts.php)を配置して functions.php で読み込む場合の例です。
ファイルの読み込みには、PHP の require_once を使用して、読み込むファイルのパスを指定します。
require_once get_template_directory() . '/inc/scripts.php'; //または、require を使用 require get_template_directory() . '/inc/scripts.php';
以下は公式テーマ「twentynineteen」の functions.php の例です。
/** SVG Icons class. */ require get_template_directory() . '/classes/class-twentynineteen-svg-icons.php'; /** Custom Comment Walker template. */ require get_template_directory() . '/classes/class-twentynineteen-walker-comment.php'; /** Enhance the theme by hooking into WordPress. */ require get_template_directory() . '/inc/template-functions.php'; /** SVG Icons related functions. */ require get_template_directory() . '/inc/icon-functions.php'; /** Custom template tags for the theme. */ require get_template_directory() . '/inc/template-tags.php'; /** Customizer additions. */ require get_template_directory() . '/inc/customizer.php';
または、get_template_part() を使って以下のようにして読み込むこともできますが、この場合、読み込まれた側のファイルで呼び出し元の functions.php で定義された変数を参照することができないので注意が必要です(テンプレートパーツで変数を参照)。この方法は、個人的にはお勧めしません。
また、get_template_part() を使う場合は、ファイルの拡張子は記述しません。
get_template_part('/inc/scripts');