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!

  • 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 Aug 5 at 14:11
  • Hi @WebElaine thank you! – ralphsmit Aug 5 at 17:58
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',
...


| improve this answer | |
  • 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.. – ralphsmit Aug 5 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 Aug 5 at 19:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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