Skip to content
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

How to register block variations with PHP in WP 6.5 #233

Closed
justintadlock opened this issue Mar 8, 2024 Discussed in #228 · 6 comments
Closed

How to register block variations with PHP in WP 6.5 #233

justintadlock opened this issue Mar 8, 2024 Discussed in #228 · 6 comments

Comments

@justintadlock
Copy link

Discussed in #228

WordPress 6.5 is shipping with a new get_block_type_variations filter hook: https://github.com/WordPress/wordpress-develop/blob/4ba0963a1f61d062d7345e429c823f3eb5ab5f78/src/wp-includes/class-wp-block-type.php#L608-L616

This essentially works as a PHP-based method of registering custom block variations. This would be a lightweight tutorial for anyone who wants to pick it up and showcase a new WP 6.5 feature. Of course, I'm always happy to take this one on myself too.

Some possible limitations I've seen in my early testing so far:

  • It doesn't seem that you can pass a custom SVG to the icon property. I believe this may only be limited to dashicons.
  • The isActive check seems to be limited to the array method of checking attributes.

Example

Here's an example variation on the Spacer block that's specific to my theme, but it's just a PoC:

add_filter('get_block_type_variations', function($variations, $block_type) {

	if ('core/spacer' === $block_type->name) {
		$variations[] = [
			'name'       =>      'x3p0/theme-spacer',
			'title'      =>      __('Spacer', 'x3p0-ideas'),
			'keywords'   =>   [ 'space', 'spacer', 'spacing' ],
			'isDefault'  => true,
			'isActive'   => [ 'height' ],
			'attributes' => [
				'height' => 'var:preset|spacing|plus-8'
			]
		];
	}

	return $variations;
});
@justintadlock
Copy link
Author

@justintadlock
Copy link
Author

Props to @ndiego for the first review.

@eidolonnight
Copy link

This looks good to me as well. Nice work @justintadlock .

@justintadlock
Copy link
Author

Thanks, @eidolonnight.

@justintadlock
Copy link
Author

justintadlock commented Mar 14, 2024

Social Copy:

WordPress 6.5 has a new hook for registering block variations with PHP. No JavaScript required. Learn more on the Developer Blog:

https://developer.wordpress.org/news/2024/03/14/how-to-register-block-variations-with-php/

@bph
Copy link
Collaborator

bph commented Apr 12, 2024

scheduled for April 23 on Social.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Status: Published (Done)
Development

No branches or pull requests

3 participants