Last active
June 8, 2023 11:26
-
-
Save phpbits/31d04e8d10e2e8af0a702ea5080d19f4 to your computer and use it in GitHub Desktop.
Component to disable dimension settings based on block context. https://jeffreycarandang.com/restrict-block-settings-based-on-post-type-user-roles-or-block-context/
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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