WordPress / developer-blog-content

In this GitHub space, WordPress team coordinate content to be published on the Developer Blog. Discussion and montly meetings (first Thu) in WP Slack #core-dev-blog
40 stars 5 forks source link

How to disable specific heading levels in the Editor #338

Open bph opened 6 hours ago

bph commented 6 hours ago

Discussed in https://github.com/WordPress/developer-blog-content/discussions/323

Originally posted by **ndiego** October 22, 2024 This would be a snippet that provides an example of the heading-level curation options available in WordPress 6.7. The snippet will show you how to disable H1, H5, and H6 in the Heading block for all users who are not administrators using PHP. ```php function example_modify_heading_levels_globally( $args, $block_type ) { $is_administrator = current_user_can( 'edit_theme_options' ); if ( 'core/heading' !== $block_type || $is_administrator ) { return $args; } // Remove H1, H5, and H6. $args['attributes']['levelOptions']['default'] = [ 2, 3, 4 ]; return $args; } add_filter( 'register_block_type_args', 'example_modify_heading_levels_globally', 10, 2 ); ```
ndiego commented 6 hours ago

The snippet is ready for a first review here: https://docs.google.com/document/d/1BOuO8eqqMfVae-wBzfItBOr5MMuuO0heY7_kJG07_OM/edit

troychaplin commented 4 hours ago

@ndiego I reviewed this, had one minor comment. Great article, I upgraded to 6.7 today and remove an older block check in lieu of this function and it's working great!