-
-
Save DavidPeralvarez/37c8c148f890d946fadb2c25589baf00 to your computer and use it in GitHub Desktop.
// Útil para filtros de Gutenberg como "allowed_block_types" | |
archives | |
audio | |
button | |
categories | |
code | |
column | |
columns | |
coverImage | |
embed | |
file | |
freeform | |
gallery | |
heading | |
html | |
image | |
latestComments | |
latestPosts | |
list | |
more | |
nextpage | |
paragraph | |
preformatted | |
pullquote | |
quote | |
reusableBlock | |
separator | |
shortcode | |
spacer | |
subhead | |
table | |
textColumns | |
verse | |
video |
<?php | |
// Filtro para limitar el tipo de bloques que admite un CPT 'libro'. | |
function scv_allowed_block_types( $allowed_block_types, $post ) { | |
if ( $post->post_type === 'libro' ) { | |
return array( | |
'core/paragraph', | |
'core/image', | |
'core/list' | |
); | |
} | |
return $allowed_block_types; | |
} | |
add_filter( 'allowed_block_types', 'scv_allowed_block_types', 10, 2 ); |
To fetch the list of all registered blocks in the JS, we can do something like this:
wp.blocks.getBlockTypes()
you may try this in browser console.
Also, to get a list of blocks for a particular namespace, you may use something like this:
wp.blocks.getBlockTypes().filter((block) => { return block.name.indexOf('core') !== -1});
(This will list all blocks with namespace 'core'
wp.blocks.getBlockTypes()
👍 👏
Hi all, is there a PHP only way (as per above example) to register only certain variations of the embed block?
@troychaplin , @parmarhardip , a bit late but for anyone else who stumbles here, i use this way to disable the (absolutely insane) list of core embeds inside an editor enqueued script
add_action('enqueue_block_editor_assets', function() {
wp_enqueue_script('theme-block-scripts', get_template_directory_uri() . '/editor.js', ['wp-blocks', 'wp-element']);
});
wp.domReady(function() {
const dumbEmbedTypes = [
'smugmug',
'wolfram-cloud',
...
];
dumbEmbedTypes.forEach(type => {
wp.blocks.unregisterBlockVariation('core/embed', type);
});
});
I had to add more hooks for js file. Allows only the 'youtube' embed.
add_action('enqueue_block_editor_assets', function() {
wp_enqueue_script('theme-block-scripts-unregister', get_template_directory_uri() . '/assets/js/admin-embed-block-unregister.js', ['wp-blocks', 'wp-element', 'wp-dom-ready', 'wp-edit-post']);
});
document.addEventListener("DOMContentLoaded", function () {
wp.domReady(function () {
const allowedEmbedBlocks = ["youtube"];
const blocks = wp.blocks.getBlockVariations("core/embed");
blocks?.forEach(function (blockVariation) {
if (-1 === allowedEmbedBlocks.indexOf(blockVariation.name)) {
wp.blocks.unregisterBlockVariation("core/embed", blockVariation.name);
}
});
});
});
The below solution works for me if the
Gutenberg
plugin is installed. Otherwise wait for the release of WP 5.7 in the next few days and those filters will be included into core.Original thread:
https://wordpress.stackexchange.com/questions/379612/how-to-remove-the-core-embed-blocks-in-wordpress-5-6/380977