Using WordPress ‘get_block_editor_theme_styles()’ PHP function

The get_block_editor_theme_styles() WordPress PHP function creates an array of theme styles to load into the block editor.

Usage

$theme_styles = get_block_editor_theme_styles();

Parameters

This function has no parameters.

More information

See WordPress Developer Resources: get_block_editor_theme_styles

Examples

Add block editor theme styles to a custom WordPress plugin

This example demonstrates how to add block editor theme styles to a custom WordPress plugin using the get_block_editor_theme_styles() function:

function my_custom_plugin_enqueue_block_editor_assets() {
    $theme_styles = get_block_editor_theme_styles();
    foreach ($theme_styles as $style) {
        wp_enqueue_style($style->handle);
    }
}
add_action('enqueue_block_editor_assets', 'my_custom_plugin_enqueue_block_editor_assets');

Modify block editor theme styles

This example shows how to modify the block editor theme styles using the get_block_editor_theme_styles() function:

function my_custom_plugin_modify_theme_styles($styles) {
    $theme_styles = get_block_editor_theme_styles();
    foreach ($theme_styles as $style) {
        if ($style->handle === 'my-target-style') {
            $style->src = 'https://example.com/new-style.css';
        }
    }
    return $styles;
}
add_filter('editor_stylesheets', 'my_custom_plugin_modify_theme_styles');

Register a new block editor theme style

This example demonstrates how to register a new block editor theme style using the get_block_editor_theme_styles() function:

function my_custom_plugin_register_theme_style() {
    $theme_styles = get_block_editor_theme_styles();
    $new_style = (object) [
        'handle' => 'my-custom-style',
        'src' => 'https://example.com/custom-style.css'
    ];
    array_push($theme_styles, $new_style);
    return $theme_styles;
}
add_filter('editor_stylesheets', 'my_custom_plugin_register_theme_style');

Remove a block editor theme style

This example shows how to remove a specific block editor theme style using the get_block_editor_theme_styles() function:

function my_custom_plugin_remove_theme_style($styles) {
    $theme_styles = get_block_editor_theme_styles();
    foreach ($theme_styles as $key => $style) {
        if ($style->handle === 'style-to-remove') {
            unset($theme_styles[$key]);
        }
    }
    return $styles;
}
add_filter('editor_stylesheets', 'my_custom_plugin_remove_theme_style');

Display a list of registered theme styles in the block editor

This example demonstrates how to display a list of registered theme styles in the block editor using the get_block_editor_theme_styles() function:

function my_custom_plugin_display_registered_theme_styles() {
    $theme_styles = get_block_editor_theme_styles();
    echo '<ul>';
    foreach ($theme_styles as $style) {
        echo '<li>' . esc_html($style->handle) . ' - ' . esc_url($style->src) . '</li>';
    }
    echo '</ul>';
}
add_action('admin_notices', 'my_custom_plugin_display_registered_theme_styles');