Skip to content

Instantly share code, notes, and snippets.

@phpbits
Last active June 8, 2023 11:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save phpbits/31d04e8d10e2e8af0a702ea5080d19f4 to your computer and use it in GitHub Desktop.
Save phpbits/31d04e8d10e2e8af0a702ea5080d19f4 to your computer and use it in GitHub Desktop.
import { select } from '@wordpress/data';
import { addFilter } from '@wordpress/hooks';
/**
* Component to disable dimension settings based on block context.
*
* @param {Object} settings - The block settings object.
* @param {string} settingsName - The name of the settings being modified.
* @param {string} clientId - The block id.
* @param {string} blockName - The block name.
* @returns {Object} The updated block settings object.
*/
const disableDimensionSettings = (settings, settingsName, clientId, blockName) => {
// Retrieve current user with capability to update settings
const { canUser } = select('core');
const currentUser = canUser('update', 'settings');
// Retrieve the current post type
const { getCurrentPostType } = select('core/editor');
const currentPostType = getCurrentPostType();
// Disable these block settings.
const disabledBlockSettings = [
'spacing.padding',
'spacing.margin',
];
if (
disabledBlockSettings.includes( settingsName ) &&
currentPostType === 'page' &&
!currentUser // Add this condition for Admin only access
) {
return false;
}
return settings;
};
addFilter(
'blockEditor.useSetting.before',
'my-plugin/disable-dimension-settings',
disableDimensionSettings
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment