Skip to content

Instantly share code, notes, and snippets.

@bahiirwa
Forked from wpmark/php-block-styles.php
Created May 11, 2022 16:30
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 bahiirwa/ff0d20959c3547a0fde1678351bb1477 to your computer and use it in GitHub Desktop.
Save bahiirwa/ff0d20959c3547a0fde1678351bb1477 to your computer and use it in GitHub Desktop.
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