WordPress
作成日時:2018/05/11
更新日時:2019/04/24
メニュー
オプションテーブル
wp_optionsテーブルに存在する以下のレコード
_transient_XXXX _site_transient_XXXX
以下の関数で操作を行う
set_transient() get_transient() set_site_transient() get_site_transient() delete_transient() delete_site_transient()
関数一覧
| 関数名 | 定義ファイル名 | 説明 |
|---|---|---|
| wp_get_current_user | wp-includes/pluggable.php | - |
get_the_terms() 使用方法
取得に成功した場合は戻り値はWP_Termの配列
取得に失敗した場合は戻り値がboolean、またはWP_Errorオブジェクトになるため、それぞれ判定
https://developer.wordpress.org/reference/functions/get_the_terms/
https://developer.wordpress.org/reference/classes/wp_error/
https://developer.wordpress.org/reference/classes/wp_term/
$terms = get_the_terms( get_the_ID(), 'taxonomy' );
if ( $terms && ! is_wp_error( $terms ) ) {
foreach( $terms as $term ) {
$term->name;
}
}
register_post_type() 使用方法
$post_typeには任意のカスタム投稿タイプ名を指定します。(最大20文字で、大文字とアンダースコアとスペースは含められません。)
$argsには特定のパラメータを配列で指定します。
https://codex.wordpress.org/Function_Reference/register_post_type
register_post_type( $post_type, $args );
フィルターフック確認関数、ショートコード
function getHookListHtml( $hook_filter = '', $func_filter = '', $priority_filter = '' ) {
global $wp_filter;
$ret = '';
$priority_filter_int = 0;
if ( $priority_filter ) {
$priority_filter_int = intval( $priority_filter );
}
if ( $hook_filter && ! array_key_exists( $hook_filter, $wp_filter ) ) {
$ret = '<p>' . $hook_filter . 'フックに登録されている関数がありません' . '</p>';
return $ret;
}
foreach ( $wp_filter as $filter_key => $filter ) {
if ( $hook_filter && $hook_filter !== $filter_key ) {
continue;
}
$ret .= '<h2>' . $filter_key . 'フックに登録されている関数' . '</h2>';
foreach ( $filter->callbacks as $priority => $callback ) {
if ( $priority_filter && $priority_filter_int !== $priority ) {
continue;
}
$ret .= '<h3>priority : ' . $priority . '</h3>';
$ret .= '<ol>';
foreach ( $callback as $func_key => $func ) {
if ( $func_filter && $func_filter !== $func_key ) {
continue;
}
$ret .= '<li>' . $func_key . '</li>';
}
$ret .= '</ol>';
}
}
return $ret;
}
function displayHookList( $atts ) {
$pairs = array(
'hook' => '',
'func' => '',
'priority' => ''
);
$atts = shortcode_atts( $pairs, $atts, 'DisplayHookList' );
return getHookListHtml( $atts['hook'], $atts['func'], $atts['priority'] );
}
add_shortcode( 'DisplayHookList', 'displayHookList' );
主要なアクションフックの実行タイミング確認関数
$action_hook_listにアクションフック名を追加し、foreachで[action hook] + アクションフック名をエラーログに出力するコールバック関数を設定しています。
$action_hook_list = array(
'after_setup_theme',
'init',
'parse_request',
'parse_query',
'send_headers',
'pre_get_posts',
'the_post',
'admin_bar_init',
'wp_loaded',
'wp',
'wp_enqueue_scripts',
'wp_print_scripts',
'template_redirect',
'wp_head',
'wp_footer',
'registered_post_type',
'registered_taxonomy',
'set_current_user',
'shutdown'
);
foreach ( $action_hook_list as $hook ) {
add_action( $hook, function() use ( $hook ) { error_log( '[action hook]' . $hook ); } );
}
https://codex.wordpress.org/Plugin_API/Action_Reference/after_setup_theme
https://codex.wordpress.org/Plugin_API/Action_Reference/init
https://codex.wordpress.org/Plugin_API/Action_Reference/parse_request
https://codex.wordpress.org/Plugin_API/Action_Reference/the_post
https://codex.wordpress.org/Plugin_API/Action_Reference/pre_get_posts