0

For a WordPress theme I deregister a few blocks for Gutenberg (in fact, I only register the ones I need).

For this, I use a filter:

add_filter( 'allowed_block_types', 'amb_allowed_block_types' );

This filter hooks into a function like this:

function amb_allowed_block_types( $allowed_blocks ) {

    return [
        'core/paragraph',
        'core/image',
        'core/heading',
        'core/gallery',
...

This works nice. But I don't want to use this filter if it is being used on a WP theme that doesn't have Gutenberg enabled. How can I make sure that this filter is only being used on Gutenberg sites?

Many thanks!

2
  • 1
    That filter won't do anything if they don't have the Block Editor enabled, so you shouldn't need to do anything additional.
    – WebElaine
    Commented Aug 5, 2020 at 14:11
  • Hi @WebElaine thank you!
    – ralphjsmit
    Commented Aug 5, 2020 at 17:58

1 Answer 1

0

You can check if the current theme supports Gutenberg blocks inside your function

function amb_allowed_block_types( $allowed_blocks ) {
    if(! current_theme_supports('wp-block-styles') ){
        return $allowed_blocks;
    }
    return [
        'core/paragraph',
        'core/image',
        'core/heading',
        'core/gallery',
...


2
  • Hi Ahmad, thank you for your help! I tried the code, but unfortunately it doesn't work. All blocks still show up, instead of the selected blocks..
    – ralphjsmit
    Commented Aug 5, 2020 at 18:02
  • in this docs developer.wordpress.org/block-editor/developers/themes/… , the themes that supports Gutenberg may use this add_theme_support( 'wp-block-styles' ); based on that we check if the theme supports 'wp-block-styles', may be the theme ignore it and use another indicator
    – Ahmad Wael
    Commented Aug 5, 2020 at 19:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.