Custom styling for WordPress Gutenberg blocks: Part 1
It’s an exciting time to be a WordPress developer, with the official release of version 5.0 just around the corner. This will mark the debut of a new editor codenamed Gutenberg. If you deal with WordPress on a regular basis, either as a developer or as a user, then you probably understand why this is big news.
Not everyone is looking forward to the new version, though, as it does bring a very different editing experience to WordPress. There is some uncertainty about how this will impact the broader WordPress ecosystem. However, the new editor has the potential to revolutionize the way you create content for your WordPress site. While it may encounter initial resistance in engaging users, I think it can ultimately create a more tangible connection with your content in a way that the classic TinyMCE editor cannot. You can try out the new editor ahead of the planned WordPress 5.0 release by installing the Gutenberg plugin from the WordPress plugin repository. If you haven’t had the chance to try it out yet, I highly recommend you do so to get a preview of your future editing experience in WordPress!
Creating content in the new editor is entirely based on blocks. Each piece of content you add to the editor is a block. This includes all your favorite elements such as sliders, paragraphs, buttons, lists, images, testimonials, and more. After adding a block to the editor, you can configure settings that control its appearance and behavior. These can be edited on the block itself or via the Inspector panel (located on the right side of the editor screen). Block settings are sometimes repeated in two locations, but this varies from block to block.
However, almost all blocks provide an option in the Inspector panel to manually add one or more CSS class names to allow further customization of the block. This can be useful if you wish to override the styling of a core block or a third-party block.
While this works fine, it would be beneficial to extend this behavior and allow for adding block customization options through a set of predefined style selections. That's exactly what block style changes bring us, and we'll focus specifically on them in this tutorial.
prerequisites
We’ll also look at how to add block style variations to your own blocks and how to extend existing blocks, so in order to proceed you’ll ideally need to be familiar with the basics of WordPress plugin development and how to create blocks.
Don't panic, though - if you need a crash course, check out my four-part tutorial on creating custom blocks. It covers pretty much everything you need to know about this tutorial - except for the block style changes, which is the focus of this particular tutorial!
Additionally, if you want to follow along with the code and try it out for yourself, you will need a local development server for running WordPress (e.g. WAMP, MAMP, etc.) and a code editor.
background
The Block Style Variants API was introduced to Project Gutenberg in plugin v3.2 and allows you to apply alternative block styles directly through the editor interface.
To get the same results before the block style changes, you must manually enter the custom CSS class in the
Additional CSS Classestext field in the Block Inspector panel, located in the Advanced part. If you are interested in the original proposal for the block style changes, then you can read the full details in the pull request in the official Gutenberg repository.
Any style variants defined for the block can be accessed directly in the block toolbar. Select the block and click the icon in the upper left corner of the toolbar to display the
Block Stylespane.
Remember earlier when I said that certain block settings
inspection groups can be accessed directly within the block? Well, that's exactly what happens with block style changes! Make sure the block is selected and viewed in the Inspector panel.
This is for convenience and you can choose the style variant from whichever suits you best. For example, on larger screens, you can choose to change block styles through the Inspector panel, as it allows you to swap between styles with a click of the mouse. However, on smaller devices you may want to hide the inspector panel and just change styles via the block toolbar.
Core Block Support
Some core blocks currently support block style variations, including:
Button
- Pull Quote
- Quote
- Separator
- Table
- I'm sure support for other core blocks will be added in the future as this feature becomes more widely adopted. It's very flexible and I'm sure many users will look forward to choosing from predefined styling options for most blocks. Once you use block style variations, it's easy to see why this happens.
Of course, you can also add block style variations to your own blocks. Next we will discuss the specific implementation details.
Implementing block style changes
There are two ways to implement custom block style changes. If you have access to the block definition, you can specify a block style variant directly within the registerBlockType()
function via the style attribute.
For example, this is what the button core block style attribute definition looks like.
styles: [ { name: 'default', label: _x( 'Rounded', 'block style' ), isDefault: true }, { name: 'outline', label: __( 'Outline' ) }, { name: 'squared', label: _x( 'Squared', 'block style' ) }, ],
Here, three new block style variants are registered. Note that the Rounded Corners style is also set as the default style.
However, if you don't have access to the block source code, there is another approach you can take. In Gutenberg 3.4, two new functions were added to register and unregister block style variants from outside the block definition.
To register a custom block style variant, use the registerBlockStyle()
function as follows:
wp.blocks.registerBlockStyle( 'core/button', { name: 'custom-button-style', label: 'My Button Style' } );
This adds a new block style variant called custom-button-style
to the core button block. Now when inserting a button block into the editor you will see the new block style variations available.
Once selected, the block style variant is added is-style-custom-add the Button-style
CSS class to the ## in the Block Inspector panel #Other CSS ClassesText fields.
registerBlockStyle() function in your own code. Don’t worry, we’ll cover the full example in the next article and you’ll be able to download the final plugin code so you can test it yourself.
When you first insert a block that supports block style variants, it's worth noting that until you specifically click on the block style variant, no CSS classes are actually added to the block tag, even if one of them appears Selected by default.
Try it yourself.
Insert the new button block into the editor and turn on the block style variation options. You should see the
Rounded Corners option selected by default. If you open the Advanced section in the block inspector, the CSS class has not been added to the text field. Therefore, CSS classes are not inserted into block tags. View the HTML output from the button block in the editor to confirm for yourself.
Now go back to the Block Style Variant settings for the Button block and click on the Default option (Selected option) or any other Block Style option. You will immediately see that the CSS class has been added to theOther CSS Classes text field and block wrapper tags. After selecting a block style variant, any custom CSS rules defined for the custom class will also be applied immediately.
This behavior is a bit confusing when first encountered, because intuitively you would expect CSS classes to be added automatically for the default selected block style variant.in conclusion
We've learned what block style variants are and why they are a useful addition to the block editing experience. I also demonstrate a basic implementation of a block style variant.
In the next article, we will detail how to register your own block styles and connect CSS via plugins as well as child themes.
We will also look at how to unregister block styles and how to set which style variant is selected by default when a block is first inserted into the editor.
The above is the detailed content of Custom styling for WordPress Gutenberg blocks: Part 1. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











There are four main error types in PHP: 1.Notice: the slightest, will not interrupt the program, such as accessing undefined variables; 2. Warning: serious than Notice, will not terminate the program, such as containing no files; 3. FatalError: the most serious, will terminate the program, such as calling no function; 4. ParseError: syntax error, will prevent the program from being executed, such as forgetting to add the end tag.

In PHP, password_hash and password_verify functions should be used to implement secure password hashing, and MD5 or SHA1 should not be used. 1) password_hash generates a hash containing salt values to enhance security. 2) Password_verify verify password and ensure security by comparing hash values. 3) MD5 and SHA1 are vulnerable and lack salt values, and are not suitable for modern password security.

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.

HTTP request methods include GET, POST, PUT and DELETE, which are used to obtain, submit, update and delete resources respectively. 1. The GET method is used to obtain resources and is suitable for read operations. 2. The POST method is used to submit data and is often used to create new resources. 3. The PUT method is used to update resources and is suitable for complete updates. 4. The DELETE method is used to delete resources and is suitable for deletion operations.

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

In PHPOOP, self:: refers to the current class, parent:: refers to the parent class, static:: is used for late static binding. 1.self:: is used for static method and constant calls, but does not support late static binding. 2.parent:: is used for subclasses to call parent class methods, and private methods cannot be accessed. 3.static:: supports late static binding, suitable for inheritance and polymorphism, but may affect the readability of the code.

PHP handles file uploads through the $\_FILES variable. The methods to ensure security include: 1. Check upload errors, 2. Verify file type and size, 3. Prevent file overwriting, 4. Move files to a permanent storage location.
