WordPress の Classic Editor 内の Enter と Shift+Enter の動作入れ替えを子テーマの functions.php でやってみた

現在使用しているテーマの functions.php を直接編集してしまうと、テーマをアップデートしたら新バージョンの functions.php で上書きされて、編集した箇所が消えてしまいます。

そこで、子テーマを用意して、子テーマの functions.php に Classic Editor 内の Enter と Shift+Enter を入れ替えるコードを追加して、親テーマがアップデートされても該当コードが消えないようにしてみました。

自分のローカル環境にディレクトリとファイルを作成

mkdir twentytwentyone-child

cd twentytwentyone-child

code style.css functions.php

子テーマのデザインカスタマイズ用 style.css

今回は、デザインをカスタマイズしませんが、子テーマを登録する時に必須となる style.css を用意します。親テーマは twentytwentyone です。

@charset "UTF-8";
/*
Theme Name: Twenty Twenty-One child
Template: twentytwentyone
Description: Twenty Twenty-One の子テーマ
Version: 1.0
*/

子テーマの functions.php

子テーマの style.css を有効かするコードと、Classic Editor 内の Enter と Shift+Enter を入れ替えるコードを用意します。

<?php
function theme_enqueue_styles() {
  wp_enqueue_style( 'parent-style', get_template_directory_uri() . '/style.css' );
  wp_enqueue_style( 'child-style', get_stylesheet_directory_uri() . '/style.css', array('parent-style') );
}
add_action( 'wp_enqueue_scripts', 'theme_enqueue_styles' );

function custom_enter( $settings ) {
 $settings[ 'forced_root_block' ] = false;
 return $settings;
}
add_filter( 'tiny_mce_before_init', 'custom_enter' );
?>

子テーマのファイル一式を zip にして新規追加し有効化

cd ..

zip -r twentytwentyone-child.zip twentytwentyone-child
タグ: