I am creating a plugin with custom gutenberg blocks.
Each block has a styles.scss file. If I add code to this file, the block is styled in both the back end and front end, as expected per the docs.
For example, in a block "section" I want to style the background color only on the back end:
.wp-block-pbdsblocks-section {
background-color: #e0e0e0;
padding: 10px;
}
But this affects both front and back end. The articles I've been reading say to apply the style by creating an "editor.scss" file in the block directory.
I import the editor.scss file into the block's index.js.
I can see my code in this file being compiled and added to /assets/css/blocks.style.css
I have the plugins' php file set to enqueue it:
/**
* Enqueue block editor only JavaScript and CSS
*/
function pbdsblocks_editor_scripts()
{
// Make paths variables so we don't write em twice ;)
$blockPath = '/assets/js/editor.blocks.js';
$editorStylePath = '/assets/css/blocks.editor.css';
// Enqueue the bundled block JS file
wp_enqueue_script(
'pbdsblocks-blocks-js',
plugins_url( $blockPath, __FILE__ ),
[ 'wp-i18n', 'wp-element', 'wp-blocks', 'wp-components', 'wp-api', 'wp-editor' ],
filemtime( plugin_dir_path(__FILE__) . $blockPath )
);
// Enqueue optional editor only styles
wp_enqueue_style(
'pbdsblocks-blocks-editor-css',
plugins_url( $editorStylePath, __FILE__),
[ 'wp-blocks' ],
filemtime( plugin_dir_path( __FILE__ ) . $editorStylePath )
);
}
// Hook scripts function into block editor hook
add_action( 'enqueue_block_editor_assets', 'pbdsblocks_editor_scripts' );
But the styles only take affect if they're in the style.scss
file. Which affects front end and back end.
add_action()
calls. One CSS file for back end only withadd_action
hooked toenqueue_block_editor_assets
and the other CSS file for both front and back end withadd_action
hooked toenqueue_block_assets
.