Skip to content

Instantly share code, notes, and snippets.

@neverything
Created March 17, 2025 06:01
Show Gist options
  • Save neverything/7786c4659b737e0e134193d6d4c24f19 to your computer and use it in GitHub Desktop.
Save neverything/7786c4659b737e0e134193d6d4c24f19 to your computer and use it in GitHub Desktop.
Add additional block styles from your child theme: https://silvanhagen.com/writing/additional-block-styles-child-theme
<?php
namespace OllieChild;
function enqueue_custom_child_block_styles() {
// Scan our styles folder to locate block styles.
$files = glob( get_stylesheet_directory() . '/assets/blocks/*.css' );
foreach ( $files as $file ) {
// Get the filename and core block name.
$filename = basename( $file, '.css' );
$block_name = str_replace( 'core-', 'core/', $filename );
wp_enqueue_block_style(
$block_name,
array(
'handle' => "ollie-child-block-{$filename}",
'src' => get_theme_file_uri( "assets/blocks/{$filename}.css" ),
'path' => get_theme_file_path( "assets/blocks/{$filename}.css" ),
)
);
}
}
add_action( 'init', __NAMESPACE__ . '\enqueue_custom_child_block_styles', 20 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment