【WordPress】タイトルセパレータを変更する方法

記事タイトルとサイト名のセパレータ(区切り文字)はデフォルトでenダッシュ()になっていますが、これを変更する方法を紹介します。

前提

まずadd_theme_support( 'title-tag' );を記述してタイトルタグが有効になっている必要があります。


add_action(
	'after_setup_theme',
	function() {
		add_theme_support( 'title-tag' );
	}
);

コード

document_title_separatorというフィルターフックが用意されていますのでそれを使用します。


add_filter( 'document_title_separator', 'change_document_title_separator', 10, 1 );
function change_document_title_separator( $sep ) {
	$sep = '|';
	return $sep;
}