Last active
March 12, 2023 19:55
-
-
Save aurooba/bfdc267f2b7404ac462a3481bc5e6a82 to your computer and use it in GitHub Desktop.
Custom block settings based on location in WordPress 6.2
This file contains 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 { addFilter } from '@wordpress/hooks'; | |
import { select } from '@wordpress/data'; | |
/** | |
* Disable text color controls on Heading blocks | |
* when placed inside of Media & Text blocks. | |
*/ | |
addFilter( | |
'blockEditor.useSetting.before', | |
'myPlugin/useSetting.before', | |
( settingValue, settingName, clientId, blockName ) => { | |
if ( blockName === 'core/heading' ) { | |
const { getBlockParents, getBlockName } = select("core/block-editor"); | |
const blockParents = getBlockParents( clientId, true ); | |
const inMediaText = blockParents.some( ( ancestorId ) => getBlockName( ancestorId ) === 'core/media-text' ); | |
if ( inMediaText && settingName === 'color.text' ) { | |
return false; | |
} | |
} | |
return settingValue; | |
} | |
); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment