Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Register WordPress block styles with PHP

Register block styles with PHP

You can now register different styles for WordPress blocks using PHP - no Javascript needed! Use the code here, either in your themes functions.php file or a plugin.

The name paramter forms part of the CSS class that is added to the block. Don't forget, this is all that block styles do, they had an additional class which then allows you to style them in a different way.

The class will take the form of is-style-{NAME}. So in the example here, when the "EyeBrow" style is applied to a heading, that heading block would have the class of is-style-eyebrow.

<?php
/**
* Register some default block editor styles for this block.
*/
function hd_testimonials_register_testimonials_block_styles() {
// add the small image style.
register_block_style(
'core/heading', // name of your block
array(
'name' => 'eyebrow', // part of the class that gets added to the block.
'label' => __( 'Eyebrow Heading', 'hd-testimonials' ),
)
);
}
add_action( 'init', 'hd_testimonials_register_testimonials_block_styles' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment