admin管理员组

文章数量:1402916

I have tried all kinds of things. Nothing worked. Not sure if it's worth posting it all but. For example:

add_action( 'enqueue_block_editor_assets', __NAMESPACE__ . '\enqueue_hide_blocks_script' );

function enqueue_hide_blocks_script(): void {
    wp_enqueue_script(
        'arve-hide-blocks', // Handle
        plugins_url( 'src/hide-blocks.js', PLUGIN_FILE ),
        array( 'wp-blocks', 'wp-dom-ready', 'wp-edit-post' ),
        VERSION,
        array( 'strategy' => 'defer' )
    );
}
wp.domReady( () => {
    const embedBlock = wp.data.select( 'core/blocks' ).getBlockType( 'core/embed' );

    console.log( 'embedBlock', embedBlock ); // empty

    if ( embedBlock && embedBlock.variations ) {
        embedBlock.variations.forEach( ( variation ) => {
            if ( [ 'youtube', 'vimeo' ].includes( variation.name ) ) {
                // Hide from inserter by overriding supports
                wp.blocks.registerBlockType( 'core/embed', {
                    ...embedBlock,
                    supports: {
                        ...embedBlock.supports,
                        inserter: false,
                    },
                } );
            }
        } );
    }
} );

I tried to get the block variations of the core/embed block in different ways, and it never worked. Also had AI give me PHP solutions that did not work.

To be clear, I only want to remove variations, not the entire core/embed block. And I only want to remove them from the inserter and not remove them completely, so already inserted blocks are not usable anymore, that I had working already.

I have tried all kinds of things. Nothing worked. Not sure if it's worth posting it all but. For example:

add_action( 'enqueue_block_editor_assets', __NAMESPACE__ . '\enqueue_hide_blocks_script' );

function enqueue_hide_blocks_script(): void {
    wp_enqueue_script(
        'arve-hide-blocks', // Handle
        plugins_url( 'src/hide-blocks.js', PLUGIN_FILE ),
        array( 'wp-blocks', 'wp-dom-ready', 'wp-edit-post' ),
        VERSION,
        array( 'strategy' => 'defer' )
    );
}
wp.domReady( () => {
    const embedBlock = wp.data.select( 'core/blocks' ).getBlockType( 'core/embed' );

    console.log( 'embedBlock', embedBlock ); // empty

    if ( embedBlock && embedBlock.variations ) {
        embedBlock.variations.forEach( ( variation ) => {
            if ( [ 'youtube', 'vimeo' ].includes( variation.name ) ) {
                // Hide from inserter by overriding supports
                wp.blocks.registerBlockType( 'core/embed', {
                    ...embedBlock,
                    supports: {
                        ...embedBlock.supports,
                        inserter: false,
                    },
                } );
            }
        } );
    }
} );

I tried to get the block variations of the core/embed block in different ways, and it never worked. Also had AI give me PHP solutions that did not work.

To be clear, I only want to remove variations, not the entire core/embed block. And I only want to remove them from the inserter and not remove them completely, so already inserted blocks are not usable anymore, that I had working already.

Share Improve this question asked Mar 13 at 20:00 NextGenThemesNextGenThemes 7568 silver badges28 bronze badges
Add a comment  | 

1 Answer 1

Reset to default -1

Trying to reregister the variations won't work as you have found. Instead, you'd want to hook into the initial core/embed block type registration and modify the parameters there. This can be done using the block.registerBlockType hook:

function alterEmbedVariations( settings, name ) {
  if ( name !== 'core/embed' ) {
    return settings;
  }

  return {
    ...settings,
    variations: settings.variations.map( ( variation ) => ( {
      ...variation,
      ...( [ 'youtube', 'vimeo' ].includes( variation.name )
        ? { scope: [] }
        : {} ),
    } ) );
  };
}

wp.hooks.addFilter(
    'blocks.registerBlockType',
    'my-plugin/embed/variations-alter',
    alterEmbedVariations
);

Note: you don't need this inside wp.domReady, as the hook system will ensure the filter function is fired at the appropriate time.

You may also need to remove the defer strategy from the script loading parameters. Timing may or may not be important for the filter function to work properly.

本文标签: