Skip to content

Instantly share code, notes, and snippets.

@elpuas
Created October 22, 2021 13:13
Show Gist options
  • Save elpuas/ed5961b2b656dd2df117800dfe514f42 to your computer and use it in GitHub Desktop.
Save elpuas/ed5961b2b656dd2df117800dfe514f42 to your computer and use it in GitHub Desktop.
Gutenberg Filter to disable block settings ( This can also be done by theme.json, actually is better )
/**
* WordPress dependencies
*/
import { addFilter } from '@wordpress/hooks';
/**
* Change the default setting for the core/button block
*
* @param {object} settings
* @param {string} name
*/
function disableSettings(settings, name) {
console.log(settings);
// if the current block is not the button, move on
if (name !== "core/button") {
return settings;
}
/**
* We create a variable that assigns the settings we want to change,
* maintaining the levels in the original settings object.
* The original settings object is as follows:
*
* supports: {
* anchor: true
* "align": false,
"alignWide": false,
* ...
* }
*
* */
let changeDefaultSettings = {
supports: {
"color": {
"background": false,
"gradients": false,
"text": false,
},
"className": false,
"anchor": false,
"align": false,
"alignWide": false,
"typography": {
"fontSize": false,
"__experimentalFontFamily": false
},
"__experimentalBorder": {
"radius": false,
"__experimentalSkipSerialization": false
},
},
};
/**
* We are merging the original settings and our new setting, which replaces only
* the setting we have a new value for, and then putting it in a
* new mergedSettings object
*
* {} : the empty object which will be stored in mergedSettings
* settings : the original settings object
* changeDefaultSettings : our modified setting object
*/
let mergedSettings = lodash.merge({}, settings, changeDefaultSettings);
// send back the mergedSettings
return mergedSettings;
}
/**
* Call the filter
*
* Filter name: blocks.registerBlockType
* Unique identifier for our change: wdsx/disableSettings
* The function that manipulated the passed data: changeDefaultSettings
*/
addFilter(
"blocks.registerBlockType",
"elpuas/disableSettings",
disableSettings
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment