Skip to content

Instantly share code, notes, and snippets.

@aurooba
Last active March 12, 2023 19:55
Show Gist options
  • Save aurooba/bfdc267f2b7404ac462a3481bc5e6a82 to your computer and use it in GitHub Desktop.
Save aurooba/bfdc267f2b7404ac462a3481bc5e6a82 to your computer and use it in GitHub Desktop.
Custom block settings based on location in WordPress 6.2
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