概要
get_default_block_editor_settings
フィルタは、WordPressのブロックエディター(Gutenberg)のデフォルト設定をカスタマイズするために使用されるフィルターフックです。このフィルタを利用すると、ブロックエディターに表示される設定を変更したり、追加したりすることができます。主に次のような機能を実装する際によく使われます:
- 新しいカスタムブロックのデフォルト設定を追加する
- 既存のブロックのデフォルト設定を変更する
- ユーザーの権限に基づいてエディターの機能を制御する
- プラグインの設定に応じてエディターの機能を有効または無効にする
- デフォルトのエディタースタイルを変更する
- メタデータを追加してエディター内のブロック挙動を制御する
- アクセシビリティに関する設定を変更する
- A/Bテストのためにエディターの動作を変更する
このフィルタは、WordPressのバージョン5.0以降で使用可能です。また、WordPressコアファイルのパスはwp-includes/block-editor.php
です。
構文
add_filter('get_default_block_editor_settings', 'your_function_name', 10, 1);
パラメータ
$settings
: ブロックエディターの設定を含む配列。
戻り値
- 修正されたブロックエディターの設定を含む配列。
関連する関数
使用可能なバージョン
- WordPress 5.0以降
コアファイルのパス
wp-includes/block-editor.php
この関数のアクションでの使用可能性
アクション | 使用可能 |
---|---|
mu_plugin_loaded | |
registered_post_type | |
plugins_loaded | |
wp_roles_init | |
setup_theme | |
after_setup_theme | |
set_current_user | |
init | |
register_sidebar | |
wp_loaded | |
send_headers | |
parse_query | |
pre_get_posts | |
wp | |
template_redirect | |
get_header | |
wp_head |
サンプルコード
サンプル1: カスタムブロックのデフォルト設定を追加
add_filter('get_default_block_editor_settings', 'custom_block_settings');
function custom_block_settings($settings) {
$settings['customBlockSetting'] = true;
return $settings;
}
このコードは、カスタム設定をブロックエディターのデフォルト設定に追加します。
サンプル2: 既存のブロック設定を変更
add_filter('get_default_block_editor_settings', 'modify_existing_block_settings');
function modify_existing_block_settings($settings) {
if (isset($settings['core/paragraph'])) {
$settings['core/paragraph']['align'] = 'justify';
}
return $settings;
}
このコードは、段落ブロックのデフォルトの配置を変更します。
サンプル3: ユーザー権限に基づく設定変更
add_filter('get_default_block_editor_settings', 'user_role_based_settings');
function user_role_based_settings($settings) {
if (!current_user_can('editor')) {
$settings['allowedBlockTypes'] = ['core/paragraph'];
}
return $settings;
}
このコードは、エディターロールでないユーザーに対して許可されるブロックタイプを制限します。
サンプル4: エディタースタイルの変更
add_filter('get_default_block_editor_settings', 'change_editor_styles');
function change_editor_styles($settings) {
$settings['styles'] = ['my-custom-style'];
return $settings;
}
このコードは、ブロックエディターのデフォルトスタイルを変更します。
サンプル5: メタデータの追加
add_filter('get_default_block_editor_settings', 'add_meta_data');
function add_meta_data($settings) {
$settings['meta'] = ['customKey' => 'customValue'];
return $settings;
}
このコードは、ブロックエディターの設定にカスタムメタデータを追加します。