Search

How to Disable Certain Social Media on Embed Block in WordPress Block Editor

WordPress block editor (Gutenberg) comes with over 70 default blocks that you can choose when you click on the + Block Inserter icon. However, you may often end up not using most of those blocks. If you want to have a cleaner Block Inserter, you can do so by disabling unneeded blocks and keeping essential ones via Block Manager.

The Embeds blocks group is one of the block groups which has the most blocks in it. The blocks in there are to embed content from various social media and media platforms some of which may be useful for you if you need them. Unfortunately, you can’t select which social media to disable since you only can disable the Embed block as a whole entity via Block Manager.

With this tutorial, we will show you an alternative way to disable the embed block you don’t need and a way to enable only the embed block you need with an example for each method. Let’s get started!

Disable Certain Embed and Enable Preferred Embed Only

Getting Started

Before you start, you may want to back up your site and use a child theme to avoid breaking the parent theme or losing your changes when you update your theme as this tutorial requires you to add some custom code in the functions.php file of your theme and add a JavaScript file in the theme folder. If you don’t mind adding one plugin to safely add custom code to your WordPress theme, you can use the Code Snippets plugin as it is an easier and faster way to add custom codes and manage them.

Add Custom Code Snippet to the Theme File

Once you’re ready, start by navigating to Appearance Theme File Editor from your WordPress admin dashboard (If you’re using a block theme, the Theme File Editor is under the Tools menu) then copy the following code to the bottom of functions.php file on the Theme Files list.

function wpb_embedblock() {
    wp_enqueue_script(
        'deny-list-blocks',
        get_template_directory_uri() . '/js/blockembed.js',
        array( 'wp-blocks', 'wp-dom-ready', 'wp-edit-post' )
    );
}
add_action( 'enqueue_block_editor_assets', 'wpb_embedblock' );

Once you’ve placed the code, save the changes by clicking on the Update File button.

The code above is for adding an action to unregister the embed block of your choice from your website by loading the JavaScript file from your theme folder which you’ll need to create after this.

Create a Script File in the Theme Folder

The next step is to create a script file in your theme folder. This file will contain a custom code with the embed block you want to unregister or the embed block you want to register.

Once you’re ready, log in to your web hosting service then open up the file manager. Continue by navigating to your theme folder /wp-content/themes/youractivetheme/js/ (if your theme doesn’t have the js folder you can create one) to create a .js file and give it the name “blockembed.js”.

Once you’ve created the file, you can proceed to select the tutorial to block certain social media embeds or enable only preferred ones.

Disabling Certain Embed Block

Disabling Only One Embed Block

To disable certain social media and media platform embeds from your embed blocks group, you can copy the following code to the .js file you’ve created from the create a script step then modify the ‘selectembed‘ to the embed of your choice.

wp.domReady(function () {
  wp.blocks.unregisterBlockVariation('core/embed', 'selectembed');
});

Let’s try removing the YouTube embed block from the group. To do so, you just need to modify the ‘selectembed‘ to ‘youtube‘. So, the code will be like follows:

wp.domReady(function () {
  wp.blocks.unregisterBlockVariation('core/embed', 'youtube');
});

Save the changes you’ve made to the file, then the YouTube embed will disappear from your Block Inserter, and it also won’t appear when you try to search for it.

Disabling More Than One Embed Block

And to remove more than one embed block, for example, we will remove Twitter, YouTube, and Vimeo from the embed block group. Then the code for your embedblock.js file will be like follows:

wp.domReady( function() {

    var embed_variations = [
                            'youtube',
                            'twitter',
                            'vimeo'
                ];

    for (var i = embed_variations.length - 1; i >= 0; i--) {
        wp.blocks.unregisterBlockVariation('core/embed', embed_variations[i]);
    }
} );

Feel free to modify the embed list in the code according to the embed block you want to remove from your website. And you can also add more items to the list by adding a comma to the previous item and then adding the block embed id below the previous item.

Enabling Preferred Embed Only

To enable preferred certain social media and media platform embeds only from your embed blocks group, you can copy the following code to the .js file you’ve created from the create a script step then modify the ‘embedlist‘ to the embed of your choice and add more embed to enable after the //add more embed after this line.

wp.domReady(function () {
  const allowedEmbedBlocks = [
    'embedlist',
    //add more embed after this line
    
  ];
  wp.blocks.getBlockVariations('core/embed').forEach(function (blockVariation) {
    if (-1 === allowedEmbedBlocks.indexOf(blockVariation.name)) {
      wp.blocks.unregisterBlockVariation('core/embed', blockVariation.name);
    }
  });
});

Let’s try enabling only YouTube and Vimeo embed blocks and remove other social media and media platform embeds from the embed group. To do so, you just need to modify the ‘embedlist‘ to ‘youtube‘ then add ‘vimeo‘ and a coma after the //add more embed after this line. So the code will be like follows:

wp.domReady(function () {
  const allowedEmbedBlocks = [
    'youtube',
    //add more embed after this
    'vimeo',
  ];
  wp.blocks.getBlockVariations('core/embed').forEach(function (blockVariation) {
    if (-1 === allowedEmbedBlocks.indexOf(blockVariation.name)) {
      wp.blocks.unregisterBlockVariation('core/embed', blockVariation.name);
    }
  });
});

Save the changes you’ve made to the file, then when you try to add an embed from the Block Inserter, only Youtube, Vimeo, and the default embed block are available to be added.

The Bottom Line

If you want to have a cleaner Block Inserter and want to remove some of the embed blocks, unfortunately, you can’t select which social media to disable since you only can disable the Embed block as a whole entity via Block Manager. However, with this tutorial, we showed you an alternative way to disable the embed block you don’t need and a way to enable only the embed block you need.

This page may contain affiliate links, which help support our project. Read our affiliate disclosure.
Hendri Risman

Hendri Risman

Hendri is a WordPress expert and a writer staff at WPPagebuilders. He writes solutions on how to get things fixed in WordPress a lot. Mostly without involving a plugin.
Want to save yearly expense up to $219? why not?

1 thought on “How to Disable Certain Social Media on Embed Block in WordPress Block Editor”

  1. Thank you for this code snippet! This works in the block editor, but once I am in the Widget page, I get this error at the top:
    “Notice: Function wp_enqueue_script() was called incorrectly. “wp-editor” script should not be enqueued together with the new widgets editor (wp-edit-widgets or wp-customize-widgets). Please see Debugging in WordPress for more information. (This message was added in version 5.8.0.) in /wp-includes/functions.php on line 5865″

    Reply

Leave a Comment