enqueue_block_styles_assets

The timeline below displays how wordpress function enqueue_block_styles_assets has changed across different WordPress versions. If a version is not listed, refer to the next available version below.

WordPress Version: 6.3

/**
 * Function responsible for enqueuing the styles required for block styles functionality on the editor and on the frontend.
 *
 * @since 5.3.0
 *
 * @global WP_Styles $wp_styles
 */
function enqueue_block_styles_assets()
{
    global $wp_styles;
    $block_styles = WP_Block_Styles_Registry::get_instance()->get_all_registered();
    foreach ($block_styles as $block_name => $styles) {
        foreach ($styles as $style_properties) {
            if (isset($style_properties['style_handle'])) {
                // If the site loads separate styles per-block, enqueue the stylesheet on render.
                if (wp_should_load_separate_core_block_assets()) {
                    add_filter('render_block', static function ($html, $block) use ($block_name, $style_properties) {
                        if ($block['blockName'] === $block_name) {
                            wp_enqueue_style($style_properties['style_handle']);
                        }
                        return $html;
                    }, 10, 2);
                } else {
                    wp_enqueue_style($style_properties['style_handle']);
                }
            }
            if (isset($style_properties['inline_style'])) {
                // Default to "wp-block-library".
                $handle = 'wp-block-library';
                // If the site loads separate styles per-block, check if the block has a stylesheet registered.
                if (wp_should_load_separate_core_block_assets()) {
                    $block_stylesheet_handle = generate_block_asset_handle($block_name, 'style');
                    if (isset($wp_styles->registered[$block_stylesheet_handle])) {
                        $handle = $block_stylesheet_handle;
                    }
                }
                // Add inline styles to the calculated handle.
                wp_add_inline_style($handle, $style_properties['inline_style']);
            }
        }
    }
}

WordPress Version: 5.9

/**
 * Function responsible for enqueuing the styles required for block styles functionality on the editor and on the frontend.
 *
 * @since 5.3.0
 *
 * @global WP_Styles $wp_styles
 */
function enqueue_block_styles_assets()
{
    global $wp_styles;
    $block_styles = WP_Block_Styles_Registry::get_instance()->get_all_registered();
    foreach ($block_styles as $block_name => $styles) {
        foreach ($styles as $style_properties) {
            if (isset($style_properties['style_handle'])) {
                // If the site loads separate styles per-block, enqueue the stylesheet on render.
                if (wp_should_load_separate_core_block_assets()) {
                    add_filter('render_block', function ($html, $block) use ($block_name, $style_properties) {
                        if ($block['blockName'] === $block_name) {
                            wp_enqueue_style($style_properties['style_handle']);
                        }
                        return $html;
                    }, 10, 2);
                } else {
                    wp_enqueue_style($style_properties['style_handle']);
                }
            }
            if (isset($style_properties['inline_style'])) {
                // Default to "wp-block-library".
                $handle = 'wp-block-library';
                // If the site loads separate styles per-block, check if the block has a stylesheet registered.
                if (wp_should_load_separate_core_block_assets()) {
                    $block_stylesheet_handle = generate_block_asset_handle($block_name, 'style');
                    if (isset($wp_styles->registered[$block_stylesheet_handle])) {
                        $handle = $block_stylesheet_handle;
                    }
                }
                // Add inline styles to the calculated handle.
                wp_add_inline_style($handle, $style_properties['inline_style']);
            }
        }
    }
}

WordPress Version: 8.2

/**
 * Function responsible for enqueuing the styles required for block styles functionality on the editor and on the frontend.
 *
 * @since 5.3.0
 */
function enqueue_block_styles_assets()
{
    $block_styles = WP_Block_Styles_Registry::get_instance()->get_all_registered();
    foreach ($block_styles as $block_name => $styles) {
        foreach ($styles as $style_properties) {
            if (isset($style_properties['style_handle'])) {
                // If the site loads separate styles per-block, enqueue the stylesheet on render.
                if (wp_should_load_separate_core_block_assets()) {
                    add_filter('render_block', function ($html) use ($style_properties) {
                        wp_enqueue_style($style_properties['style_handle']);
                        return $html;
                    });
                } else {
                    wp_enqueue_style($style_properties['style_handle']);
                }
            }
            if (isset($style_properties['inline_style'])) {
                // Default to "wp-block-library".
                $handle = 'wp-block-library';
                // If the site loads separate styles per-block, check if the block has a stylesheet registered.
                if (wp_should_load_separate_core_block_assets()) {
                    $block_stylesheet_handle = generate_block_asset_handle($block_name, 'style');
                    global $wp_styles;
                    if (isset($wp_styles->registered[$block_stylesheet_handle])) {
                        $handle = $block_stylesheet_handle;
                    }
                }
                // Add inline styles to the calculated handle.
                wp_add_inline_style($handle, $style_properties['inline_style']);
            }
        }
    }
}

WordPress Version: 8.1

/**
 * Function responsible for enqueuing the styles required for block styles functionality on the editor and on the frontend.
 *
 * @since 5.3.0
 */
function enqueue_block_styles_assets()
{
    $block_styles = WP_Block_Styles_Registry::get_instance()->get_all_registered();
    foreach ($block_styles as $block_name => $styles) {
        foreach ($styles as $style_properties) {
            if (isset($style_properties['style_handle'])) {
                // If the site loads separate styles per-block, enqueue the stylesheet on render.
                if (wp_should_load_separate_core_block_assets()) {
                    add_filter('render_block', function ($html, $block) use ($style_properties) {
                        wp_enqueue_style($style_properties['style_handle']);
                        return $html;
                    });
                } else {
                    wp_enqueue_style($style_properties['style_handle']);
                }
            }
            if (isset($style_properties['inline_style'])) {
                // Default to "wp-block-library".
                $handle = 'wp-block-library';
                // If the site loads separate styles per-block, check if the block has a stylesheet registered.
                if (wp_should_load_separate_core_block_assets()) {
                    $block_stylesheet_handle = generate_block_asset_handle($block_name, 'style');
                    global $wp_styles;
                    if (isset($wp_styles->registered[$block_stylesheet_handle])) {
                        $handle = $block_stylesheet_handle;
                    }
                }
                // Add inline styles to the calculated handle.
                wp_add_inline_style($handle, $style_properties['inline_style']);
            }
        }
    }
}

WordPress Version: 5.3

/**
 * Function responsible for enqueuing the styles required for block styles functionality on the editor and on the frontend.
 *
 * @since 5.3.0
 */
function enqueue_block_styles_assets()
{
    $block_styles = WP_Block_Styles_Registry::get_instance()->get_all_registered();
    foreach ($block_styles as $styles) {
        foreach ($styles as $style_properties) {
            if (isset($style_properties['style_handle'])) {
                wp_enqueue_style($style_properties['style_handle']);
            }
            if (isset($style_properties['inline_style'])) {
                wp_add_inline_style('wp-block-library', $style_properties['inline_style']);
            }
        }
    }
}