WordPress / developer-blog-content

In this GitHub space, WordPress team coordinates content to be published on the Developer Blog. Discussions and montly meetings (first Thu - 13:00 UTC) in WP Slack #core-dev-blog
https://developer.wordpress.org/news
40 stars 5 forks source link

How to disable specific heading levels in the Editor #322

Closed ndiego closed 1 month ago

ndiego commented 1 month ago

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.

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 );