{"id":990,"date":"2022-05-11T10:08:54","date_gmt":"2022-05-11T10:08:54","guid":{"rendered":"https:\/\/mklasen.com\/?p=990"},"modified":"2022-05-11T10:09:21","modified_gmt":"2022-05-11T10:09:21","slug":"registering-block-styles-in-wordpress-with-php","status":"publish","type":"post","link":"https:\/\/mklasen.com\/registering-block-styles-in-wordpress-with-php\/","title":{"rendered":"Registering block styles in WordPress with PHP"},"content":{"rendered":"\n
Sometimes you just want a block to show up a little different than the options that WordPress, or your theme provides by default. In those cases it’s nice and easy to just add a simple PHP plugin, or add a few lines of code to your functions.php<\/p>\n\n\n\n
It’s possible to influence the various options for block styling in PHP, when hooking into the The code below adds a “clean” button option to the button block. After adding the code, it shows up in the block editor and when you select it the block is rendered with an extra CSS class: is-style-clean.<\/p>\n\n\n\n You can save the code above in a PHP file and upload it in your after_setup_theme<\/code> action.<\/p>\n\n\n\n
The code<\/h2>\n\n\n\n
add_action(\n\t'after_setup_theme',\n\tfunction() {\n\t\tregister_block_style(\n\t\t\t'core\/button',\n\t\t\tarray(\n\t\t\t\t'name' => 'clean',\n\t\t\t\t'label' => __( 'Clean' ),\n\t\t\t)\n\t\t);\n\t}\n);<\/code><\/pre>\n\n\n\n
The code in a plugin<\/h2>\n\n\n\n
'clean',\n\t\t\t\t'label' => __( 'Clean' ),\n\t\t\t)\n\t\t);\n\t}\n);<\/code><\/pre>\n\n\n\n
wp-content\/plugins<\/code>folder, or compress it in a ZIP file and upload that ZIP file in your WordPress dashboard.<\/p>\n\n\n\n
Looking to build a plugin from scratch?<\/h2>\n\n\n\n