1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

WordPress検証コード

Last updated at Posted at 2020-03-16

概要

WordPressのテーマ・プラグインの検証をするときによく使うコードをまとめます。
開発よりもむしろ全体的なチューニングでよく使うものです。

ログ出力

ログをファイルに書き出す。
/wp-content/debug.log にログが書き出される。

これをwp-config.phpに書く

define( 'WP_DEBUG', true );
if ( WP_DEBUG ) {
    define( 'WP_DEBUG_LOG', true );
    define( 'WP_DEBUG_DISPLAY', true );
    @ini_set( 'display_errors', 1 );
    define( 'SAVEQUERIES', true );
}

ログ出力

error_log('test message');

Hook

掛かっているフックを調べる

現状どのようなフックが掛かっているかを確認する。
ある程度は何がどんなフックをかけているか把握できる。

add_action('wp_footer', function(){
	global $wp_filter;
	echo "<pre>";
	var_dump($wp_filter['wp_footer']);
	echo "</pre>";
});

どのタイミングで起こるのかを確認

WordPressの基本的なアクションフックが始まるごとにログを出力する。結構力技。

テーマのfunctions.php上ではロードが遅いので、プラグインから動かすのがおすすめ。

add_action('muplugins_loaded',function(){error_log('[hook] muplugins_loaded');},0);
add_action('registered_taxonomy',function(){error_log('[hook] registered_taxonomy');},0);
add_action('registered_post_type',function(){error_log('[hook] registered_post_type');},0);
add_action('plugins_loaded',function(){error_log('[hook] plugins_loaded');},0);
add_action('sanitize_comment_cookies',function(){error_log('[hook] sanitize_comment_cookies');},0);
add_action('setup_theme',function(){error_log('[hook] setup_theme');},0);
add_action('load_textdomain',function(){error_log('[hook] load_textdomain');},0);
add_action('after_setup_theme',function(){error_log('[hook] after_setup_theme');},0);
add_action('auth_cookie_malformed',function(){error_log('[hook] auth_cookie_malformed');},0);
add_action('auth_cookie_valid',function(){error_log('[hook] auth_cookie_valid');},0);
add_action('set_current_user',function(){error_log('[hook] set_current_user');},0);
add_action('init',function(){error_log('[hook] init');},0);
add_action('register_sidebar',function(){error_log('[hook] register_sidebar');},0);
add_action('wp_register_sidebar_widget',function(){error_log('[hook] wp_register_sidebar_widget');},0);
add_action('wp_default_scripts',function(){error_log('[hook] wp_default_scripts');},0);
add_action('wp_default_styles',function(){error_log('[hook] wp_default_styles');},0);
add_action('admin_bar_init',function(){error_log('[hook] admin_bar_init');},0);
add_action('add_admin_bar_menus',function(){error_log('[hook] add_admin_bar_menus');},0);
add_action('wp_loaded',function(){error_log('[hook] wp_loaded');},0);
add_action('parse_request',function(){error_log('[hook] parse_request');},0);
add_action('send_headers',function(){error_log('[hook] send_headers');},0);
add_action('parse_query',function(){error_log('[hook] parse_query');},0);
add_action('pre_get_posts',function(){error_log('[hook] pre_get_posts');},0);
add_action('posts_selection',function(){error_log('[hook] posts_selection');},0);
add_action('wp',function(){error_log('[hook] wp');},0);
add_action('template_redirect',function(){error_log('[hook] template_redirect');},0);
add_action('get_header',function(){error_log('[hook] get_header');},0);
add_action('wp_enqueue_scripts',function(){error_log('[hook] wp_enqueue_scripts');},0);
add_action('twentyeleven_enqueue_color_scheme',function(){error_log('[hook] twentyeleven_enqueue_color_scheme');},0);
add_action('wp_head',function(){error_log('[hook] wp_head');},0);
add_action('wp_print_styles',function(){error_log('[hook] wp_print_styles');},0);
add_action('wp_print_scripts',function(){error_log('[hook] wp_print_scripts');},0);
add_action('get_search_form',function(){error_log('[hook] get_search_form');},0);
add_action('loop_start',function(){error_log('[hook] loop_start');},0);
add_action('the_post',function(){error_log('[hook] the_post');},0);
add_action('get_template_part_content',function(){error_log('[hook] get_template_part_content');},0);
add_action('loop_end',function(){error_log('[hook] loop_end');},0);
add_action('get_sidebar',function(){error_log('[hook] get_sidebar');},0);
add_action('dynamic_sidebar',function(){error_log('[hook] dynamic_sidebar');},0);
add_action('get_search_form',function(){error_log('[hook] get_search_form');},0);
add_action('pre_get_comments',function(){error_log('[hook] pre_get_comments');},0);
add_action('wp_meta',function(){error_log('[hook] wp_meta');},0);
add_action('get_footer',function(){error_log('[hook] get_footer');},0);
add_action('get_sidebar',function(){error_log('[hook] get_sidebar');},0);
add_action('twentyeleven_credits',function(){error_log('[hook] twentyeleven_credits');},0);
add_action('wp_footer',function(){error_log('[hook] wp_footer');},0);
add_action('wp_print_footer_scripts',function(){error_log('[hook] wp_print_footer_scripts');},0);
add_action('admin_bar_menu',function(){error_log('[hook] admin_bar_menu');},0);
add_action('wp_before_admin_bar_render',function(){error_log('[hook] wp_before_admin_bar_render');},0);
add_action('wp_after_admin_bar_render',function(){error_log('[hook] wp_after_admin_bar_render');},0);
add_action('shutdown',function(){error_log('[hook] shutdown');},0);

スタイル・スクリプト

キューされているスクリプト一覧を見る

add_action( 'wp_print_scripts', function() {
    echo "<pre>";
    var_dump(wp_scripts()->queue);
    echo "</pre>";
}, 0);

スタイル


add_action( 'wp_print_scripts', function() {
    echo "<pre>";
    var_dump(wp_styles()->queue);
    echo "</pre>";
}, 100);

ポストメタ


add_action('add_meta_boxes', function(){
    add_meta_box(
        'asashimo',
        'post_metas',
        'asashimo_render',
        null,
        'normal',
        'high',
    );
});

function asashimo_render() {
    echo "<pre>";
    var_dump(get_post_meta(get_the_ID()));
    echo "</pre>";
}

JS

おまけ

スクロール量検出

add_action('wp_footer', function(){
    ?>
    <div
        id="asashimo"
        style="
            position: fixed;
            left: 0;
            background-color: yellow;
            width: 10em;
            height: 55px;
            padding: 5px;
        "
    >
        <span id="asashimo_scroll_y"></span><br/>
        <span id="asashimo_width"></span>
    </div>
    <script type="text/javascript">
        ;(function(){
            let fix = () => {
                document.getElementById('asashimo').style.top = window.innerHeight - 80 + 'px';
            }
            let w = () => {
                document.getElementById('asashimo_width').innerHTML = 'width: ' + window.innerWidth + 'px';
            }
            let y = () => {
                document.getElementById('asashimo_scroll_y').innerHTML = 'scroll-y: ' + window.pageYOffset + 'px';
            }
            window.addEventListener('DOMContentLoaded', fix, false);
            window.addEventListener('DOMContentLoaded', w, false);
            window.addEventListener('DOMContentLoaded', y, false);
            window.addEventListener('resize', fix, false);
            window.addEventListener('resize', w, false);
            window.addEventListener('scroll', y, false);
        })();
    </script>
    <?php
});
1
0
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
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?