Skip to content

Instantly share code, notes, and snippets.

@jnicol
Last active January 18, 2024 16:00
Show Gist options
  • Star 7 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save jnicol/cbbc10deee06526a3f6d296ce82c74de to your computer and use it in GitHub Desktop.
Save jnicol/cbbc10deee06526a3f6d296ce82c74de to your computer and use it in GitHub Desktop.
Register/unregister WordPress Gutenberg block styles
/**
* Scripts to run when in WordPress Gutenberg editor
*
* Unregister any block styles we don't want user to be able to select
* or register our own custom block styles.
*/
wp.domReady( () => {
// Unregister any block styles we don't want user to be able to select
wp.blocks.unregisterBlockStyle( 'core/quote', 'default' );
wp.blocks.unregisterBlockStyle( 'core/quote', 'large' );
wp.blocks.unregisterBlockStyle( 'core/button', 'default' );
wp.blocks.unregisterBlockStyle( 'core/button', 'outline' );
wp.blocks.unregisterBlockStyle( 'core/button', 'squared' );
// Add custom block styles:
wp.blocks.registerBlockStyle("core/button", {
name: "mycustomstyle",
label: "My Custom Style"
});
} );
<?php
function enqueue_block_editor_scripts() {
wp_enqueue_script(
'theme-block-editor-js',
get_stylesheet_directory_uri() . '/js/blocks/block-editor-scripts.js',
array( 'wp-blocks', 'wp-dom-ready', 'wp-edit-post' ), // specify dependencies to avoid race condition
'1.0',
true
);
}
add_action('enqueue_block_editor_assets', 'enqueue_block_editor_scripts');
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment