Skip to content

Instantly share code, notes, and snippets.

@nathanrice
Last active April 11, 2020 14:36
Show Gist options
  • Save nathanrice/8ea2116df44465c733745575a1e174e4 to your computer and use it in GitHub Desktop.
Save nathanrice/8ea2116df44465c733745575a1e174e4 to your computer and use it in GitHub Desktop.
A simple proof-of-concept for modularizing the CSS for each block, and linking it "just in time".
<?php
// this snippet requires PHP 5.3+
add_action( 'wp_enqueue_scripts', function() {
wp_register_style( 'atomic-blocks/ab-cta', '/path/to/atomic-blocks/css/ab-cta.css', array(), 1.0.0 );
} );
add_filter( 'render_block', function( $block_content, $block ) {
if ( 'atomic-blocks/ab-cta' === $block['blockName'] ) {
ob_start();
wp_print_styles( $block['blockName'] );
$block_content = ob_get_clean() . $block_content;
}
return $block_content;
}, 10, 2 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment