wp_setup_widgets_block_editor()


WordPressのwp_setup_widgets_block_editor()関数は、ウィジェットブロックエディタを初期化し、設定するために使用されます。この関数は、ブロックエディタを使用してウィジェットを管理する際に役立ちます。

構文

wp_setup_widgets_block_editor();
  • wp_setup_widgets_block_editor()は引数を取りません。

例1: ウィジェットブロックエディタの初期化

ウィジェットブロックエディタを初期化する基本的な例です。

add_action( 'init', 'setup_widgets_block_editor' ); function setup_widgets_block_editor() { wp_setup_widgets_block_editor(); }

例2: ウィジェットエリアにブロックエディタを適用

特定のウィジェットエリアにブロックエディタを適用する例です。

add_action( 'widgets_init', 'register_custom_widget_area' ); function register_custom_widget_area() { register_sidebar( array( 'name' => 'カスタムウィジェットエリア', 'id' => 'custom-widget-area', 'before_widget' => '<div id="%1$s" class="widget %2$s">', 'after_widget' => '</div>', 'before_title' => '<h2 class="widgettitle">', 'after_title' => '</h2>', ) ); wp_setup_widgets_block_editor(); }

例3: ウィジェットブロックエディタの無効化

ウィジェットブロックエディタを無効化する例です。

add_filter( 'use_widgets_block_editor', '__return_false' );

例4: カスタムウィジェットブロックの追加

カスタムウィジェットブロックを追加する例です。

add_action( 'init', 'register_custom_widget_block' ); function register_custom_widget_block() { register_block_type( 'custom/widget-block', array( 'editor_script' => 'custom-widget-block-editor', 'editor_style' => 'custom-widget-block-editor', 'style' => 'custom-widget-block', ) ); wp_setup_widgets_block_editor(); }

例5: ウィジェットブロックエディタのスタイル変更

ウィジェットブロックエディタのスタイルを変更する例です。

add_action( 'enqueue_block_editor_assets', 'custom_widget_block_editor_styles' ); function custom_widget_block_editor_styles() { wp_enqueue_style( 'custom-widget-editor-style', get_template_directory_uri() . '/css/custom-widget-editor.css', array(), '1.0.0' ); wp_setup_widgets_block_editor(); }

例6: ウィジェットブロックエディタのスクリプト追加

ウィジェットブロックエディタにカスタムスクリプトを追加する例です。

add_action( 'enqueue_block_editor_assets', 'custom_widget_block_editor_scripts' ); function custom_widget_block_editor_scripts() { wp_enqueue_script( 'custom-widget-editor-script', get_template_directory_uri() . '/js/custom-widget-editor.js', array( 'wp-blocks', 'wp-element' ), '1.0.0', true ); wp_setup_widgets_block_editor(); }

例7: ウィジェットブロックエディタの設定変更

ウィジェットブロックエディタの設定を変更する例です。

add_filter( 'widget_block_editor_settings', 'custom_widget_block_editor_settings' ); function custom_widget_block_editor_settings( $settings ) { $settings['maxWidth'] = 1200; return $settings; } wp_setup_widgets_block_editor();

注意点

  • wp_setup_widgets_block_editor()は、WordPress 5.8以降で利用可能です。
  • この関数は、ウィジェットブロックエディタを有効にするために使用されますが、テーマやプラグインによっては無効化される場合があります。

関連機能: