最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

block editor - How do I check if I can use the allowed_block_types filter?

programmeradmin9浏览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!

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!

Share Improve this question asked Aug 5, 2020 at 13:50 ralphjsmitralphjsmit 4026 silver badges23 bronze badges 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
Add a comment  | 

1 Answer 1

Reset to default 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',
...


发布评论

评论列表(0)

  1. 暂无评论