Block Editor PHP Beginner

Add a Custom Block Style

Add your own style option, like "Brand outline" for buttons, to a core block's Styles panel.

Tested on WordPress 7.1 PHP 8.3 No plugin needed Updated
functions.php 13 lines
add_action( 'init', function (): void {
	register_block_style( 'core/button', [
		'name'         => 'brand-outline',
		'label'        => 'Brand outline',
		'inline_style' => '
			.wp-block-button.is-style-brand-outline .wp-block-button__link {
				background: transparent;
				color: #6F58CA;
				border: 2px solid #6F58CA;
			}
		',
	] );
} );

Where to add this

Recommended A code snippets plugin Survives theme changes and is easy to switch off.
Child theme At the end of your child theme's functions.php file.
Must-use plugin As a .php file in wp-content/mu-plugins, starting with <?php.

Always add PHP to a staging site first, or have FTP or file manager access ready, so you can undo it if something goes wrong.

Block styles are one-click design variations in the block sidebar, like Fill and Outline for buttons. Adding your own lets editors use on-brand designs without touching colour settings.

How it works

register_block_style() adds a style to any block. Selecting it adds the class is-style-{name} to the block. The inline_style CSS is loaded on the front end and in the editor whenever the block is used, so you don’t need a separate stylesheet.

Things to watch

  • The style’s CSS needs enough specificity to beat the block’s own styles. Starting selectors with .wp-block-button.is-style-… usually does.
  • Block themes can also define styles in theme.json or /styles/blocks/ files.

How to test it

Add a Button block. In the sidebar under Styles, “Brand outline” should appear and change the button’s look.

This website uses cookies to enhance your browsing experience and ensure the site functions properly. By continuing to use this site, you acknowledge and accept our use of cookies.

Accept All Accept Required Only