Skip to content

An example of a simple plugin that adds a block style to Gutenberg. Forked to a Javascript version that works with later versions of Wordpress.

License

Notifications You must be signed in to change notification settings

audunmb/gutenberg-block-styles

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

36 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Gutenberg Block Styles Plugin

Overview

This repository is an introduction to one of the simplest forms of customization in the editor: Block Styles. Block Styles just add an extra classname to a block, so they're relatively simple to create and customize.

Block Styles Example

This repository is a WordPress plugin that includes a single custom block style. It's fairly barebones, and is meant to provide a boilerplate for more complicated plugins. The code here is a lightweight introduction to Gutenberg block customization, and doesn't require you to mess around with npm, themes, php, or (much) JavaScript.

All you really need to get started is:

  • The courage to edit a few lines in a single Javascript file.
  • Knowledge of CSS.
  • A WordPress site to upload this plugin to.

Customization

Adding + editing block styles is a three step process:

1. Open up the custom-block-styles.js file and adjust the block type, name, and label for your new block style.

For example, the built-in example adds a "Blue Paragraph" block style to the core Paragraph block:

/* Register styles */

wp.blocks.registerBlockStyle('core/paragraph', {
	name: 'blue-paragraph',
	label: wp.i18n.__( 'Blue Paragraph', 'textdomain' ),
});```

Here's another example, adding an "Awesome Cover" style to the Cover block:

/* Register styles */

wp.blocks.registerBlockStyle('core/cover', {
	name: 'awesome-cover',
	label: wp.i18n.__( 'Awesome Cover', 'textdomain' ),
});```
  • The block name on the first line should is the block you apply the style to. It's in the format source/name. Source is either core for the regular blocks or the name of the plugin/theme that adds the block. Wordpress Core Blocks reference gives the name for all the core blocks.
  • The name Property should be lowercase letters with hyphens. It is used to generate the class for your block style (see below). Make sure it's unique enough to avoid conflicts.
  • The label Property should be human readable, and probably translatable. (I'll add more on translation to this plugin at some point).

If you'd like to add multiple block styles in the same plugin, duplicate those lines and replace the Properties.

2. From there, add the CSS to style your new block style.

Block style classnames are automatically created using the following format:

.is-style-[name]

[name] maps to the name: field from step 1. So the classnames for the two examples above would be:

.is-style-blue-paragraph .is-style-awesome-cover

Open up the custom-block-style.css file, and add any CSS styles for your block. Anything you declare should be added to both the front and back end automatically.

3. Test your changes.

Zip up the plugin with your changes and upload to your site. 🎉

More Documentation

About

An example of a simple plugin that adds a block style to Gutenberg. Forked to a Javascript version that works with later versions of Wordpress.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • PHP 67.2%
  • CSS 20.3%
  • JavaScript 12.5%