What's wp ?

What's wp ?

eycatch

バックトレース

テハ
テハ
プラグインやテーマはどのファイルからロードされているのにゃ?
ワープ
ワープ
呼び出し元を調べるにはdebug_print_backtrace()を使えばいいにゃ。ローカル環境なら他のユーザーがアクセスしないのでファイルにログ出力して確認できるにゃ。

Must-Useプラグイン

ワープ
ワープ
wp-content/mu-plugins/の下に以下のコードのPHPファイルを置けば、Must-Useプラグインがロードされた呼び出し元がWordPressのインストールしたルートのdebug.logというファイル名に出力されるようになるにゃ。

wp-content/mu-plugins/mu-example.php

<?php
/*
 * Plugin Name: mu-example
 */

// ログ出力ファイルのパス定義
define( 'DEBUG_LOG_FILE', ABSPATH . "debug.log" );

// デバッグ用ログファイルが巨大にならないようあれば削除
if ( file_exists( DEBUG_LOG_FILE ) )
{
	unlink( DEBUG_LOG_FILE );
}

// バックトレースをデバッグ用ログファイルに出力
ob_start();
echo "*** mu-plugin ***\n";
debug_print_backtrace();
error_log(ob_get_clean(), 3, DEBUG_LOG_FILE);

debug.log

*** mu-plugin ***
#0  include_once() called at [...\wp-settings.php:302]
#1  require_once(...\wp-settings.php) called at [...\wp-config.php:77]
#2  require_once(...\wp-config.php) called at [...\wp-load.php:37]
#3  require_once(...\wp-load.php) called at [...\wp-blog-header.php:13]
#4  require(...\wp-blog-header.php) called at [...\index.php:17]
ワープ
ワープ
debug_backtrace()というのもあるにゃ。でもdebug_print_backtrace()の方がシンプルな出力なので読みやすいにゃ。出力をファイル先に変えるためにob_start()で出力先をバッファにしてob_get_clean()でバッファ内容を取得できるにゃ。それをerror_logでファイルに出力しているにゃ。
メリー
メリー

プラグインやテーマの呼び出し順

ワープ
ワープ
他の場所にも同様に書けば色々なところの呼び出し元がわかるにゃ。

バック・トレースをデバッグ用ログ出力

// バックトレースをデバッグ用ログファイルに出力
ob_start();
echo "*** メッセージ ***\n";
debug_print_backtrace();
error_log(ob_get_clean(), 3, DEBUG_LOG_FILE);

debug.log

*** mu-plugin ***
#0  include_once() called at [...\wp-settings.php:302]
#1  require_once(...\wp-settings.php) called at [...\wp-config.php:77]
#2  require_once(...\wp-config.php) called at [...\wp-load.php:37]
#3  require_once(...\wp-load.php) called at [...\wp-blog-header.php:13]
#4  require(...\wp-blog-header.php) called at [...\index.php:17]
*** plugin ***
#0  include_once() called at [...\wp-settings.php:371]
#1  require_once(...\wp-settings.php) called at [...\wp-config.php:77]
#2  require_once(...\wp-config.php) called at [...\wp-load.php:37]
#3  require_once(...\wp-load.php) called at [...\wp-blog-header.php:13]
#4  require(...\wp-blog-header.php) called at [...\index.php:17]
*** child theme functions.php ***
#0  include() called at [...\wp-settings.php:508]
#1  require_once(...\wp-settings.php) called at [...\wp-config.php:77]
#2  require_once(...\wp-config.php) called at [...\wp-load.php:37]
#3  require_once(...\wp-load.php) called at [...\wp-blog-header.php:13]
#4  require(...\wp-blog-header.php) called at [...\index.php:17]
*** parent theme functions.php ***
#0  include() called at [...\wp-settings.php:508]
#1  require_once(...\wp-settings.php) called at [...\wp-config.php:77]
#2  require_once(...\wp-config.php) called at [...\wp-load.php:37]
#3  require_once(...\wp-load.php) called at [...\wp-blog-header.php:13]
#4  require(...\wp-blog-header.php) called at [...\index.php:17]
*** template ***
#0  include() called at [...\wp-includes\template-loader.php:106]
#1  require_once(...\wp-includes\template-loader.php) called at [...\wp-blog-header.php:19]
#2  require(...\wp-blog-header.php) called at [...\index.php:17]
ワープ
ワープ
ぱっと見わかりにくいので図にするとこんな感じにゃ。
テハ
テハ
WordPressコアのPHPファイルの役割が見えてくるのにゃ。

アクションフックの呼び出し元

テハ
テハ
試しに親テーマのfunctions.phpのアクション・フックに登録する関数の中に書いてみたのにゃ。

親テーマのfunctions.php

function twentytwenty_register_styles() {
ob_start();
echo "*** action: wp_enqueue_scripts ***\n";
debug_print_backtrace();
error_log(ob_get_clean(), 3, DEBUG_LOG_FILE);
	$theme_version = wp_get_theme()->get( 'Version' );
	wp_enqueue_style( 'twentytwenty-style', get_stylesheet_uri(), array(), $theme_version );
	wp_style_add_data( 'twentytwenty-style', 'rtl', 'replace' );
	// Add output of Customizer settings as inline style.
	wp_add_inline_style( 'twentytwenty-style', twentytwenty_get_customizer_css( 'front-end' ) );
	// Add print CSS.
	wp_enqueue_style( 'twentytwenty-print-style', get_template_directory_uri() . '/print.css', null, $theme_version, 'print' );
}
add_action( 'wp_enqueue_scripts', 'twentytwenty_register_styles' );

debug.log

*** action: wp_enqueue_scripts ***
#0  twentytwenty_register_styles() called at [...\wp-includes\class-wp-hook.php:287]
#1  WP_Hook->apply_filters(, Array ([0] => )) called at [...\wp-includes\class-wp-hook.php:311]
#2  WP_Hook->do_action(Array ([0] => )) called at [...\wp-includes\plugin.php:484]
#3  do_action(wp_enqueue_scripts) called at [...\wp-includes\script-loader.php:2294]
#4  wp_enqueue_scripts() called at [...\wp-includes\class-wp-hook.php:287]
#5  WP_Hook->apply_filters(, Array ([0] => )) called at [...\wp-includes\class-wp-hook.php:311]
#6  WP_Hook->do_action(Array ([0] => )) called at [...\wp-includes\plugin.php:484]
#7  do_action(wp_head) called at [...\wp-includes\general-template.php:2884]
#8  wp_head() called at [...\wp-content\themes\child\index.php:9]
#9  include(...\wp-content\themes\child\index.php) called at [...\wp-includes\template-loader.php:106]
#10 require_once(...\wp-includes\template-loader.php) called at [...\wp-blog-header.php:19]
#11 require(...\wp-blog-header.php) called at [...\index.php:17]
ワープ
ワープ
ぱっと見わかりにくいので図にするとこんな感じにゃ。
ワープ
ワープ
子テーマのテンプレートの中でwp_head()を呼び出して、wp_headアクション・フックが発生し、その中でwp_enqueue_scriptsアクション・フックが発生し、親テーマのfunctions.phpでアクション・フックに登録した関数が呼び出されているのがわかるにゃ。つまり、テンプレートでwp_head()を書かないと、wp_enqueue_scriptsアクション・フックが発生しないことがわかるにゃ。
テハ
テハ
にゃるほど、発生するアクション・フックのタグは固定されたものじゃないのにゃ。

コメントはこちらから