Last active
August 3, 2022 14:36
-
-
Save drdogbot7/b09c1c88fe1199b605f66b234bf252ea to your computer and use it in GitHub Desktop.
Adding classes to wordpress paragraph, list and heading blocks
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
/** | |
* Add class names to core blocks that normally don't have them | |
* | |
* This does work, but any legacy p, ul, ol, h* will be invalid and will have to be recovered. | |
*/ | |
const setExtraPropsToBlockType = (props, blockType, attributes) => { | |
const notDefined = | |
typeof props.className === 'undefined' || !props.className ? true : false; | |
if (blockType.name === 'core/heading') { | |
return Object.assign(props, { | |
className: notDefined | |
? `wp-block-heading wp-block-heading--h${attributes.level}` | |
: `wp-block-heading wp-block-heading--h${attributes.level} ${props.className}`, | |
}); | |
} | |
if (blockType.name === 'core/list') { | |
return Object.assign(props, { | |
className: notDefined | |
? `wp-block-list wp-block-list--${ | |
attributes.ordered ? 'ordered' : 'unordered' | |
}` | |
: `wp-block-list wp-block-list--${ | |
attributes.ordered ? 'ordered' : 'unordered' | |
} ${props.className}`, | |
}); | |
} | |
if (blockType.name === 'core/paragraph') { | |
return Object.assign(props, { | |
className: notDefined | |
? 'wp-block-paragraph' | |
: `wp-block-paragraph ${props.className}`, | |
}); | |
} | |
return props; | |
}; | |
wp.hooks.addFilter( | |
'blocks.getSaveContent.extraProps', | |
'salvia/block-filters', | |
setExtraPropsToBlockType | |
); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment