Skip to content

Docs: Add Documentation for Adding Block Variations Using get_block_type_variations Hook #68434

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Conversation

Sukhendu2002
Copy link
Contributor

Issue: #63182

What?

This PR updates the Block Editor Handbook under the "Block API Reference > Variations" section to include detailed instructions on how to add block variations via PHP using the get_block_type_variations hook.

Changes Made:

  • Added a new subsection titled "Adding Block Variations from PHP."
  • Explained the get_block_type_variations hook and its parameters.
  • Included a practical PHP code example demonstrating how to add a custom variation to the core/navigation-link block.

@Sukhendu2002 Sukhendu2002 changed the title Update doc to add PHP strategy to register block variations Docs: Add Documentation for Adding Block Variations Using get_block_type_variations Hook Dec 31, 2024
@Sukhendu2002 Sukhendu2002 marked this pull request as ready for review December 31, 2024 11:09
Copy link

github-actions bot commented Dec 31, 2024

The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the props-bot label.

If you're merging code through a pull request on GitHub, copy and paste the following into the bottom of the merge commit message.

Co-authored-by: Sukhendu2002 <sukhendu2002@git.wordpress.org>
Co-authored-by: juanmaguitar <juanmaguitar@git.wordpress.org>

To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook.

@Mamaduka Mamaduka added [Type] Developer Documentation Documentation for developers [Feature] Block Variations Block variations, including introducing new variations and variations as a feature labels Jan 13, 2025
Here's an example of how to register a custom variation for the `core/navigation-link` block:

```php
function my_custom_navigation_link_variations( $variations, $block_type ) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think a better example would be the following one:

function my_custom_image_variation( $variations, $block_type ) {
    // Only modify variations for the navigation-link block
    if ( 'core/image' !== $block_type->name ) {
        return $variations;
    }

    // Add a custom variation
    $variations[] = array(
		'name'        => 'wide-image',
		'title'       => __( 'Wide image', 'textdomain' ),
		'description' => __( 'A wide image', 'textdomain' ),
		'scope'       => array( 'inserter' ),
		'isDefault'   => false,
		'attributes'  => array(
			'align' => 'wide', // Identifies the link type as custom
		),
    );

    return $variations;
}
add_filter( 'get_block_type_variations', 'my_custom_image_variation', 10, 2 );

As a 'core/image' variation doesn't need to be the children of another to be inserted (as it happens with core/navigation-link that can only be inserted inside of a ´core/navigation´)
Also in the example above there's a visual change that clarifies the use this variation.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey @juanmaguitar, Thanks for the review! I appreciate the clarification. I've updated the example code accordingly to better reflect the correct usage of block variations for core/image.

@juanmaguitar
Copy link
Contributor

Thank you very much @Sukhendu2002 for working on this.
I have added a tip callout to recommend a very relevant blog post in the WP Developer Blog.
Other than that, it's good to go! 🛳️

@juanmaguitar juanmaguitar merged commit c856964 into WordPress:trunk Feb 18, 2025
59 checks passed
@github-actions github-actions bot added this to the Gutenberg 20.4 milestone Feb 18, 2025
Kallyan01 pushed a commit to Kallyan01/gutenberg that referenced this pull request Feb 24, 2025
…ype_variations Hook (WordPress#68434)

* Update doc to add PHP strategy to register block variations

* Update example code

* Add callout with link to relevant blog post

---------

Co-authored-by: JuanMa <juanma.garrido@gmail.com>
chriszarate pushed a commit to chriszarate/gutenberg that referenced this pull request Jul 1, 2025
…ype_variations Hook (WordPress#68434)

* Update doc to add PHP strategy to register block variations

* Update example code

* Add callout with link to relevant blog post

---------

Co-authored-by: JuanMa <juanma.garrido@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Feature] Block Variations Block variations, including introducing new variations and variations as a feature [Type] Developer Documentation Documentation for developers
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants