この記事は最後に更新してから1年以上経過しています。

Gutenbergエディターのフォントを変える

説明

Gutenbergエディターを試していてしっくりこないのが、編集時とそれを投稿した際の見た目の違い。GutenbergエディターとテーマTwenty Seventeenとではまったく異なるタイプのフォントが適用されていることが大きな理由である。

font-familyの違いを確認

まずはそれぞれにどんなフォントが指定されているのか確認していこう。Gutenbergエディターでは編集要素が細分化されているが、デモページで多数出現するタイトルと本文部分のfont-familyはどちらも「"Noto Serif",serif」が指定されている。

// 以下font-familyのみ抜粋
.editor-post-title__block .editor-post-title__input {
    font-family: "Noto Serif",serif;
}

.editor-block-list__block {
    font-family: "Noto Serif",serif;
}

これに対してテーマTwenty Seventeenでは、主要部分のfont-familyが「sans-serif」となっている(要素別に個別指定もある)。

Twenty Seventeenの主要部分は「sans-serif」
// 以下font-familyのみ抜粋
html {
    font-family: sans-serif;
}

Gutenbergエディターのfont-familyを変える

異なる系統のfont-familyが指定されていることがわかったところで、Gutenbergエディターの指定を変更し、「しっくりこない」感を緩和しようと思う。Gutenbergエディターのcssを調整する際に利用できるアクションはいくつか存在しているが、ここではGutenbergエディター自身がJavaScriptやCSSファイルの読み込みを行うgutenberg_editor_scripts_and_styles関数(/gutenberg/lib/client-assets.php)が最後に実行するenqueue_block_editor_assetsアクションを利用する。テーマのfunctions.php向けのサンプルは次の通り。

add_action( 'enqueue_block_editor_assets', 'set_mytheme_font_family' );

function set_mytheme_font_family() {
	$inline =
".gutenberg-editor-page .editor-post-title__block,
.gutenberg-editor-page .editor-post-title__input,
.gutenberg-editor-page .editor-block-list__block {
    font-family: %s;
}";
	wp_add_inline_style( 'wp-edit-post', sprintf( $inline, 'sans-serif' ) );
}

Gutenbergエディターの表示は次のように変わる。

Gutenbergエディターのタイトルと共通ブロックに「sans-serif」を適用

フォントサイズや領域の違いはあるが、違和感はかなり和らいだと思う。

おまけ

さてgutenberg_editor_scripts_and_styles関数の中身を見てみると、block_editor_settingsフィルターが気になった。

$editor_settings = apply_filters( 'block_editor_settings', $editor_settings, $post );

パラメータ$editor_settingsは連想配列で、エディターに関連するさまざまな設定を調整できるようになっている。

キー内容
alignWidefalse
availableTemplates連想配列(キーがテンプレートファイル名、値がテンプレート名)
allowedBlockTypestrue
disableCustomColorsfalse)
disablePostFormatsfalse
titlePlaceholder"タイトルを追加"
bodyPlaceholder"本文をここに書く"
isRTLfalse
autosaveInterval10
maxUploadFileSize※環境依存値
allowedMimeTypes連想配列(キーがファイル拡張子、値がそのMIMEタイプ名)
stylesスタイル要素を含んだ配列

$editor_settingsの内容はこのフィルターの実行後にインラインスクリプトとして出力されるようになっており、Gutenbergエディターをカスタマイズする際には活用できそうだ。


最終更新 : 2018年10月16日 13:17


お勧め

wp_revisions_to_keep(2023年4月4日 更新)

int wp_revisions_to_keep( WP_Post $post )
投稿情報のリビジョン数を取得する。

get_month_link(2012年2月2日 更新)

string get_month_link( mixed $year, mixed $month )
月アーカイブのURLを取得する。

get_site_icon_url(2020年4月2日 更新)

string get_site_icon_url( [ int $size = 512 [ , string $url = '' [ , int $blog_id = 0 ] ] ] )
サイトアイコンのURLを取得する。

is_singular(2026年8月20日 更新)

bool is_singular( [ mixed $post_types = '' ] )
要求されているページが、投稿またはページか調べる。

wp_strip_all_tags(2018年5月27日 更新)

string wp_strip_all_tags( string $string [ , bool $remove_breaks = false ] )
文字列からHTMLタグを取り除く。