【XWRITE】独自アクションフック・フィルターフック一覧

実際に使用した商品をレビューしています。アフィリエイト広告リンクを含みます。
【XWRITE】独自アクションフック・フィルターフック一覧
初心者

XWRITEの独自フックを知りたい..
サンプルのコードも欲しいな…

そんな人向けに、WordPressテーマXWRITEの独自フックを調べてみました。

この記事を書いた人
  • IT業界24年のベテラン エンジニア
  • WordPressで企業サイトの構築しています
  • 取り扱いWordPressテーマは300個オーバー
SWELLマニアの かんた です
目次

XWRITEの独自アクションフック一覧

XWRITEの独自アクションフック

アクションフックは基本的に次のような書き方で任意の処理を実行できます。

サンプルコード

function 関数名() {
    // 実行したい処理
    echo 'Hello, WordPress!';
}

add_action( 'フック名', '関数名' );
かんた

アクションフックを個別に紹介します↓

xwrite_header_logo_before

ヘッダーロゴ(.siteInfo)直前のフックです。

ヘッダーロゴ画像を設置していない場合はサイト名が表示され、フックもサイト名直前に出力されます。

xwrite_header_logo_after

ヘッダーロゴ(.siteInfo)直後のフックです。サイトロゴは表示しつつ、その横にサイト名をテキストで表示するといった用途に活用できます。

カスタマイザー上でヘッダーロゴ画像を設置している場合でそのロゴ横にサイト名を表示するサンプルコードです。

サンプルコード

function add_kanta_site_name() {
    echo '<div class="kanta-site-name">' . get_bloginfo( 'name' ) . '</div>';
}
add_action( 'xwrite_header_logo_after', 'add_kanta_site_name' );

xwrite_contents_top

.contentsの直前

対象テンプレート
archive、index、page、search,single

xwrite_main_top

場所
.contentst直下

対象テンプレート
archive、index、page、search,single

xwrite_main_bottom

.contentsの最終要素

xwrite_page_top

固定ページコンテンツ(.articleBody)の直前

xwrite_page_bottom

固定ページコンテンツ(.articleBody)の直後

xwrite_post_top

投稿ページコンテンツ(.articleBody)の直後

xwrite_post_bottom

投稿ページコンテンツ(.articleBody)の直後

xwrite_search_title_bottom

検索ページh1(.contentsHeader__caption)の直下


XWRITEの独自フィルターフック一覧

xwrite_archive_title

カテゴリーページなどアーカイブテンプレートのタイトルをフィルターするフィルター。

xwrite_clipboard_copied_txt

投稿記事したに表示されるコピーボタンをクリックしたさいのメッセージ内容を編集するフィルター。

標準では「URLをコピーしました!」となるが、これを「コピー完了」とする場合は次のように記述する。

function my_custom_clipboard_message_tanak( $message ) {
	return 'コピー完了'; // ここを好きな文言に変更
}
add_filter( 'xwrite_clipboard_copied_txt', 'my_custom_clipboard_message_tanak' );

URLをコピーボタンはカスタマイザー内「投稿/固定ページ」→「SNSシェアボタン」、「URLコピー」にチェックが入っている場合のみ表示される。

xwrite_comment_count

投稿ページ下部にあるコメントの「コメント一覧(n件数)」部分を編集するフィルター。

xwrite_comment_title

投稿ページ下部にあるコメントの「コメントを残す」を編集するフィルター。

「コメントを残す」を「この商品のレビューをどうぞ」に変更するには次のように書く。

function post_type_specific_comment_title_tanak( $title ) {
		return 'この商品へのレビューをどうぞ';
	return $title;
}
add_filter( 'xwrite_comment_title', 'post_type_specific_comment_title_tanak' );

xwrite_content_width

メインコンテンツエリアの幅を変更するフィルター。

xwrite_description_excerpt_length

メタディスクリプションを生成する際に使われる抜粋の長さ(文字数)をフィルタリングするためのフック。デフォルトの抜粋文字数が変更でき、メタディスクリプションの長さを自由に調整できます。

function my_custom_meta_description_length_tanak( $length ) {
	return 150; // 150文字に変更
}
add_filter( 'xwrite_description_excerpt_length', 'my_custom_meta_description_length_tanak' );

xwrite_get_search_title

検索結果ページのタイトルを変更するフィルター。

テキスト検索時に、「『●●』」の検索結果」と表示されるが、絞り込み検索などでキーワード検索を使っていない場合などの調整が可能。、単純に「検索結果」と表示を変更するには次のように書く。

function my_simple_search_title_tanak( $title ) {
    return '検索結果';
}
add_filter( 'xwrite_get_search_title', 'my_simple_search_title_tanak' );

キーワード検索があった場合の処理は変更せず、キーワード入力がない場合に「検索結果」と表示するには次のように書く。

function my_simple_search_title_tanak( $title ) {
    return '検索結果';
}
add_filter( 'xwrite_get_search_title', 'my_simple_search_title_tanak' );

xwrite_hidden_term_description

アーカイブページでカテゴリやタグなどの「説明文(term description)」を表示するかどうかを制御するフィルター。

  • true が返れば → 説明文は表示されない(非表示)
  • false が返れば → 説明文が表示される(表示)

全対象ページで非表示にするには次のように書く。

function always_hide_term_description_tanak( $hidden ) {
	return true; // すべてのケースで非表示にする
}
add_filter( 'xwrite_hidden_term_description', 'always_hide_term_description_tanak' );

xwrite_inline_css_heading

関数 xw_inline_css_heading() が生成した カスタム見出しのインラインCSS を、他のテーマやプラグインから 加工・追加・削除できるようにするためのフィルター。

h2見出しにカスタムスタイルを追加するには次のように書く。

function add_custom_heading_styles( $css ) {
    $custom_css = '.articleBody h2.custom-heading { font-family: "Arial Black"; color: red; }';
    return $css . ' ' . $custom_css;
}
add_filter( 'xwrite_inline_css_heading', 'add_custom_heading_styles' );

xwrite_is_page_title_label

記事タイトルの下に表示されるラベルを表示するかどうかを外部から制御するためのフィルター。

固定ページ(スラッグ:about)だけ表示するには次のように書く。

function show_page_title_label_on_about_page_atnak( $show ) {
	if ( is_page( 'about' ) ) {
		return true;
	}
	return $show;
}
add_filter( 'xwrite_is_page_title_label', 'show_page_title_label_on_about_page_atnak' );

xwrite_is_title_layout_label

xwrite_meta_description

各ページの meta description(メタディスクリプション)を出力前にカスタマイズするためのフック。

説明文の末尾にサイト名を追加する場合は次のように書く。

function my_custom_meta_description_tanak( $description ) {
	$site_name = get_bloginfo( 'name' );
	return $description . '|' . $site_name;
}
add_filter( 'xwrite_meta_description', 'my_custom_meta_description_tanak' );

xwrite_mod_ads_text

投稿・固定ページの広告ラベル(小)のテキスト内容を編集するフィルター。カスタマイザー上では全投稿を一括で指定するが、特定の記事などの編集はこのフィルターで行える。例えば、カテゴリー○○に属する記事のみ表示を「スポンサー」に変更する場合は次のように書く。

function change_ads_text_to_sponsor_tanak( $text ) {
    if ( is_single() && in_category( 'product' ) ) {
        return 'スポンサー';
    }
    return $text;
}
add_filter( 'xwrite_mod_ads_text', 'change_ads_text_to_sponsor_tanak' );

xwrite_mod_ads_type

広告ラベルのスタイルを編集するフィルター。出力されるhtmlタグは標準でnotice-label__smallsmall部分の任意のクラスに変更するには次のように書く。

function change_ads_type_to_pr_tanak( $type ) {
    return 'pr';
}
add_filter( 'xwrite_mod_ads_type', 'change_ads_type_to_pr_tanak' );

xwrite_mod_show_ads_label

広告ラベル(小)の表示・非表示を編集するフィルター。カスタマイザー上の設定を無視し投稿ページ上で強制的に広告ラベルを表示させるには次のように書く。

function force_show_ads_label_tanak( $is_show ) {
    return true;
}
add_filter( 'xwrite_mod_show_ads_label', 'force_show_ads_label_tanak' );

xwrite_mod_show_article_meta

htmlタグarticle-meta内に表示する要素のフィルター。

キー名表示内容
post_date投稿日
modified_date更新日
post_author著者
post_categoryカテゴリ
post_commentコメント数
post_pvPV数

カテゴリー1に属する記事には公開日とPVを表示するには次のように書く。

function show_only_date_and_pv_for_category1_tanak( $parts ) {
	if ( is_single() && in_category( 1 ) ) {
		return array( 'post_date', 'post_pv' );
	}
	return $parts;
}
add_filter( 'xwrite_mod_show_article_meta', 'show_only_date_and_pv_for_category1_tanak' );

xwrite_mv_class

メインビジュアルに付与される HTML クラス名(CSSクラス)を外部から変更・追加するフィルター。

mv-contactとクラス名を追加するには次のように書く。

function add_mv_class_for_contact_page_tanak( $classes ) {
		$classes .= ' mv-contact';
	return $classes;
}
add_filter( 'xwrite_mv_class', 'add_mv_class_for_contact_page_tanak' );

xwrite_notice_area

通知エリア(header-notice)に表示されるhtmlタグを編集するフィルター。

xwrite_notice_message

通知エリア(header-notice)に表示されるテキストを編集するフィルター。

function my_notice_custom_html_atnak( $html ) {
	return '<span class="header-notice__body">📢 サイトメンテナンスは5/10に実施されます。</span>';
}
add_filter( 'xwrite_notice_message', 'my_notice_custom_html_atnak' );

xwrite_override_custom_canonical

<head> 内に出力される canonical タグの URL を編集するフィルター。newsスラッグのカテゴリーページのURLを指定するには次のように書く。

function my_custom_canonical_for_category_tanak( $canonical ) {
	if ( is_category( 'news' ) ) {
		return 'https://example.com/custom-news-canonical/';
	}
	return $canonical;
}
add_filter( 'xwrite_override_custom_canonical', 'my_custom_canonical_for_category_tanak' );

xwrite_post_excerpt_body

抜粋 ($excerpt_body) をカスタマイズするフィルター。

xwrite_post_excerpt_length

抜粋の最大文字数を変更するフィルター。

20文字に変更するには次のように書く。

function set_fixed_excerpt_length_atnak( $length ) {
	return 20; 
}
add_filter( 'xwrite_post_excerpt_length', 'set_fixed_excerpt_length_atnak' );

xwrite_post_excerpt_password_require

パスワード保護された投稿に対して抜粋が表示されない場合のメッセージを編集するフィルター。

パスワード保護中の記事の抜粋を「この投稿は保護されています。内容を表示するにはパスワードが必要です。」にする場合は次のように書く。

function change_protected_excerpt_message_atnak( $message ) {
	return 'この投稿は保護されています。内容を表示するにはパスワードが必要です。';
}
add_filter( 'xwrite_post_excerpt_password_require', 'change_protected_excerpt_message_atnak' );

xwrite_post_list_class

xw_post_list_class() 関数が出力する「HTML クラス文字列」を加工・変更するためのフィルター。

xwrite_sharebtn_class_

SNSシェアボタンのラッパーに付くクラス名をカスタマイズするフィルター。例えばcustom-share-footerとクラス名を追加するには次のように書く。

function my_custom_sharebtn_class_footer_atnak( $class_string ) {
	$class_string .= ' custom-share-footer';
	return $class_string;
}
add_filter( 'xwrite_sharebtn_class_footer', 'my_custom_sharebtn_class_footer_atnak' );

xwrite_sharebtn_class_list_

SNSシェアボタンのリスト(ul)に付与されるCSSクラスを編集するフィルター。custom-list-classとクラス名を追加するには次のように書く。

function my_custom_sharebtn_list_class_footer_atnak( $class_string ) {
    $classes = explode( ' ', $class_string );
    $classes[] = 'custom-list-class';
    return implode( ' ', $classes );
}
add_filter( 'xwrite_sharebtn_class_list_footer', 'my_custom_sharebtn_list_class_footer_atnak' );

xwrite_show_custom_tax_post_bottom

カスタム投稿記事下部にターム情報を表示させるフィルター。

次のようにコードを追加すると記事が属するターム一覧が表示される。

function show_custom_taxonomy_at_footer_tanak( $display ) {
	return true;
}
add_filter( 'xwrite_show_custom_tax_post_bottom', 'show_custom_taxonomy_at_footer_tanak' );

xwrite_show_custom_tax_post_head

カスタム投稿タイトル下にターム情報を表示させるフィルター。

カスタム投稿(blog)のタイトル下にターム一覧を表示させるには次のように書く。

function show_custom_taxonomy_for_books_only_atnak( $display ) {
    if ( get_post_type() === 'blog' ) {
        return true; // 投稿タイプが `blog` の場合のみカスタムタクソノミーを表示
    }
    return false; // その他の投稿タイプでは表示しない
}
add_filter( 'xwrite_show_custom_tax_post_head', 'show_custom_taxonomy_for_books_only_atnak' );

xwrite_skin_activate_button

ダッシュボード内、「XWRITE設定」→ 「スキン設定 」→「スキン選択」ページのボタン上にあるテキストを編集するフィルター。標準じは「スキンを有効化する」。変更するには次のように書く。

function customize_skin_activate_button_for_admins_atnak( $label ) {
    if ( current_user_can( 'manage_options' ) ) {
        return '変更する';
    }
    return $label;
}
add_filter( 'xwrite_skin_activate_button', 'customize_skin_activate_button_for_admins_atnak' );

xwrite_skin_activate_inherit_button

write_skin_activate_button と同様に、ボタンに表示するテキストを動的に変更するフィルター。

xwrite_skin_detail_description

スキンの詳細画面に表示される「説明テキスト」部分をカスタマイズするフィルター。

xwrite_skin_detail_layout

スキンの「レイアウト情報」部分に表示されるテキストを編集するフィルター

xwrite_term_description

カテゴリ・タグなどのアーカイブページで説明文をカスタマイズするフィルター。

xwrite_title_tag

ページタイトルの最終出力をカスタマイズするためのフィルター。

まとめ

WordPressテーマXWRITEのアクションフック、フィルターフックを紹介しました。

フックのメリットは、管理画面上にはない設定が行える点やテンプレートを直接編集する作業が不要で子テーマ化していない環境でもカスタマイズが行えます。

かんた

ぜひフックを活用しカスタマイズしてみてください!

本記事は記事執筆時に利用可能な最新バージョン(2.9.18.1)を調査した内容です。

よかったらシェアしてね!
  • URLをコピーしました!
目次