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.
1 Answer
Reset to default -1Trying 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.
本文标签:
版权声明:本文标题:php - How do I hide block YouTube, Vimeo block variations from the Block inserter but keep already inserted blocks usable 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744348962a2601935.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论