Skip to content

Instantly share code, notes, and snippets.

@soderlind
Last active October 19, 2019 22:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save soderlind/93208fa1130668537024a950bd85dbf9 to your computer and use it in GitHub Desktop.
Save soderlind/93208fa1130668537024a950bd85dbf9 to your computer and use it in GitHub Desktop.
add_theme_support( 'disable_block_style') .. hide gutenberg block style.
/**
* Hide block styles
*/
wp.domReady(() => {
if (_.isObject(oDelBlockStyles)) {
_.map(oDelBlockStyles, (styleVariationName, blockName) => {
styleVariationName.forEach((style) => {
wp.blocks.unregisterBlockStyle(blockName, style);
});
});
}
});
// find blocks styles
wp.domReady(() => {
// find blocks styles
wp.blocks.getBlockTypes().forEach((block) => {
if (_.isArray(block['styles'])) {
console.log(block.name, _.pluck(block['styles'], 'name'));
}
});
});
<?php
require_once 'get_theme_support_block_style.php';
add_theme_support( 'disable_block_style', [
'cap' => 'manage_options', // hide if you don't have this capability, eg. hide for Editors ...
'core/image' => [
'circle-mask',
],
'core/button' => [
'default',
'fill',
'outline',
'squared',
],
] );
<?php
add_action( 'enqueue_block_assets', 'get_theme_support_block_style', 11 );
function get_theme_support_block_style() {
if ( ! current_theme_supports( 'disable_block_style' ) ) {
return false;
}
$disable_block_style = get_theme_support( 'disable_block_style' )[0];
if ( ! is_array( $disable_block_style ) ) {
return false;
}
if ( isset( $disable_block_style['cap'] ) and current_user_can( $disable_block_style['cap'] ) ) {
return false;
}
unset( $disable_block_style['cap'] );
wp_enqueue_script( 'remove-block-style', get_stylesheet_directory_uri() . '/block-script.js', [ 'wp-blocks', 'wp-edit-post' ] );
wp_localize_script( 'remove-block-style', 'oDelBlockStyles', $disable_block_style );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment