In WordPress 7.0 and newer, you can use a per-block custom CSS input field to add exactly that: custom CSS for a particular block. And there’s currently no easy way to disable it. Thus, I’ll show you how to do it, even though it is relatively hidden within the “Advanced” settings panel of each block.

There’s two options to disable the custom CSS input field. One via PHP and one via JavaScript. Both come down to explicitly set the customCSS support property to false to tell WordPress that custom CSS is not supported by the blocks.

PHP

/**
 * Disable custom CSS in single blocks.
 * 
 * @param	array{supports: array{supports: array{customCSS?: bool}}}	$metadata Current metadata
 * @return	array{supports: array{supports: array{customCSS?: bool}}} Updated metadata
 */
function disable_block_custom_css( array $metadata ): array {
	$metadata['supports']['customCSS'] = false;
	
	return $metadata;
}

\add_filter( 'block_type_metadata', 'disable_block_custom_css' );
Code language: PHP (php)

JavaScript

wp.hooks.addFilter(
    'blocks.registerBlockType',
    'epiphyt/disable-custom-css',
    disableCustomCSS
);

function disableCustomCSS( settings, name ) {
	if ( ! settings.supports ) {
		settings.supports = {};
	}
	
	settings.supports.customCSS = false;
	
	return settings;
}Code language: JavaScript (javascript)

You can also check for $metadata['name'] in PHP or settings.name in JavaScript to check for specific types of blocks to disable the custom CSS field only for them.

Likes

Reposts

Leave a Reply

Your email address will not be published. Required fields are marked *

To respond on your own website, enter the URL of your response which should contain a link to this post's permalink URL. Your response will then appear (possibly after moderation) on this page. Want to update or remove your response? Update or delete your post and re-enter your post's URL again. (Learn more about webmentions)