Skip to content

Instantly share code, notes, and snippets.

@damiencarbery
Last active August 17, 2022 10:02
Show Gist options
  • Save damiencarbery/49cbd76c30b5116fecb7a30d0439142a to your computer and use it in GitHub Desktop.
Save damiencarbery/49cbd76c30b5116fecb7a30d0439142a to your computer and use it in GitHub Desktop.
Add custom block styles - Easily enhance the block editor with custom block styles. https://www.damiencarbery.com/2022/04/add-custom-block-styles/
/* Highlight paragraph style. */
p.is-style-highlight, .editor-styles-wrapper p.is-style-highlight { font-size: 180%; }
/* Rotated image style. */
.is-style-rotated img { transform: rotate(180deg); }
/* Group block Info and Warning styles. */
.is-style-group-info, .is-style-group-warning { margin: 0 0 10px; }
.is-style-group-info, .is-style-group-warning { padding: 2em; min-height: 35px; }
.is-style-group-info strong, .is-style-group-warning strong { color: #000000; font-weight: bold; }
.is-style-group-warning p:last-child, .is-style-group-info p:last-child { margin-bottom: 0; }
.is-style-group-info { border: 1px solid #e2e2ba; }
.is-style-group-warning { border: 1px solid #e0b1b1; }
.is-style-group-info { color: #ada771; background: #f9f9dc; border: 1px solid #fbfbef; }
.is-style-group-warning { color: #ad7676; background: #ffd9d5; border: 1px solid #fceeec; }
<?php
/*
Plugin Name: Add custom block styles
Plugin URI: https://www.damiencarbery.com/2022/04/add-custom-block-styles/
Description: Easily enhance the block editor with custom block styles. Inspired by https://briangardner.com/border-block-style/.
Author: Damien Carbery
Version: 0.4
*/
// See list of core blocks at: https://developer.wordpress.org/block-editor/reference-guides/core-blocks/
// Add custom styles to some core blocks.
function dcwd_register_custom_block_styles() {
$style_handle = 'dcwd-custom-block-styles';
wp_register_style( $style_handle, plugins_url( 'dcwd-custom-block-styles.css', __FILE__ ) );
// Block themes do not automatically enqueue the stylesheet when the custom block
// style is used. This appear to be a core bug (#55184).
// The workaround is to always enqueue the file for those themes.
if ( wp_is_block_theme() ) {
wp_enqueue_style( $style_handle );
error_log( 'wp_enqueue_style $style_handle' );
}
// Add 'Highlight' stye to paragraph block.
register_block_style(
'core/paragraph',
array(
'name' => 'highlight',
'label' => 'Highlight',
//'inline_style' => 'p.is-style-highlight { font-size: 180%; }',
'style_handle' => $style_handle,
)
);
// Add 'Rotated' style to Image block
register_block_style(
'core/image',
array(
'name' => 'rotated',
'label' => 'Rotated',
//'inline_style' => '.is-style-rotated img { transform: rotate(180deg); }',
'style_handle' => $style_handle,
)
);
// Add 'Info' style to Group block
register_block_style(
'core/group',
array(
'name' => 'group-info',
'label' => 'Info',
'style_handle' => $style_handle,
)
);
// Add 'Warning' style to Group block
register_block_style(
'core/group',
array(
'name' => 'group-warning',
'label' => 'Warning',
'style_handle' => $style_handle,
)
);
}
add_action('init', 'dcwd_register_custom_block_styles' );
// Enqueue the CSS file so that the styles can be seen in the block editor.
add_action( 'enqueue_block_editor_assets', 'dcwd_enqueue_block_editor_styles' );
function dcwd_enqueue_block_editor_styles() {
wp_enqueue_style( 'dcwd-custom-block-styles', plugin_dir_url( __FILE__ ) . 'dcwd-custom-block-styles.css' );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment