钩子文档

block_editor_settings

💡 云策文档标注

概述

block_editor_settings 是一个 WordPress 过滤器,用于修改传递给块编辑器的设置数组。该过滤器已在 WordPress 5.8.0 版本中被弃用,建议使用 block_editor_settings_all 替代。

关键要点

  • 过滤器名称:block_editor_settings
  • 参数:$editor_settings(默认编辑器设置数组)和 $post(正在编辑的 WP_Post 对象)
  • 弃用信息:自 WordPress 5.8.0 起弃用,推荐使用 block_editor_settings_all 过滤器
  • 用途:允许开发者自定义块编辑器的配置,如移除特定功能或添加自定义参数

代码示例

// 示例1:移除 DropCap 选项
add_filter( 'block_editor_settings', function( $editor_settings ) {
    $editor_settings['__experimentalFeatures']['global']['typography']['dropCap'] = false;
    return $editor_settings;
} );

// 示例2:添加自定义参数以跟踪侧边栏活动状态
function prefix_custom_editor_settings( $settings, $post ) {
    $settings['isSidebarActive'] = FALSE;
    if ( is_active_sidebar( 'sidebar-1' ) ) {
        $settings['isSidebarActive'] = TRUE;
    }
    return $settings;
}
add_filter( 'block_editor_settings', 'prefix_custom_editor_settings', 10, 2 );

注意事项

  • 此过滤器已弃用,新开发应优先使用 block_editor_settings_all 以确保兼容性
  • 修改设置时需注意数组结构,避免破坏编辑器功能
  • 自定义参数可在 JavaScript 中通过 wp.data.select('core/editor').getEditorSettings() 访问

📄 原文内容

Filters the settings to pass to the block editor.

Parameters

$editor_settingsarray
Default editor settings.
$postWP_Post
Post being edited.

Source

$editor_settings = apply_filters_deprecated( 'block_editor_settings', array( $editor_settings, $post ), '5.8.0', 'block_editor_settings_all' );

Changelog

Version Description
5.8.0 Deprecated. Use the ‘block_editor_settings_all’ filter instead.
5.0.0 Introduced.

User Contributed Notes

  1. Skip to note 3 content

    /**
     * Remove DropCap option from settings panel.
     * 
     * @param   array       $settings   Default editor settings. 
     * @return  array       $settings   Filtered block editor settings.
     */
    add_filter( 'block_editor_settings', function( $editor_settings ) {
    	$editor_settings['__experimentalFeatures']['global']['typography']['dropCap'] = false;
    	return $editor_settings;
    } );

  2. Skip to note 4 content

    /**
     * Adds a custom parameter to the editor settings that is used
     * to track whether the main sidebar has widgets.
     *
     * @param 	array   	$settings 	Default editor settings.
     * @param 	WP_Post 	$post 		Post being edited.
     * @return 	array 		$settings 	Filtered block editor settings.
     */
    function prefix_custom_editor_settings( $settings, $post ) {
    	$settings['isSidebarActive'] = FALSE;
    	
    	// Determines whether a sidebar is in use.
    	if ( is_active_sidebar( 'sidebar-1' ) ) {
    		$settings['isSidebarActive'] = TRUE;
    	} // End If Statement
    	
    	return $settings;
    }
    add_filter( 'block_editor_settings', 'prefix_custom_editor_settings', 10, 2 );
    ( function() {
    	/**
    	 * Check if the main sidebar is active (has widgets).
    	 *
    	 * This uses a custom property `isSidebarActive` added via the
    	 * `block_editor_settings` filter.
    	 *
    	 * @return 	{boolean} 	Whether sidebar is active.
    	 */
    	const sidebarIsActive = () => {
    		let settings = wp.data.select( 'core/editor' ).getEditorSettings();
    
    		if ( settings.hasOwnProperty( 'isSidebarActive' ) && !! settings.isSidebarActive ) {
    			return true;
    		} // End If Statement
    
    		return false;
    	};
    } )();