Created
May 6, 2020 14:48
-
-
Save florianbrinkmann/10561fd9c4bcfe23ed43d442c6243f76 to your computer and use it in GitHub Desktop.
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
/** | |
* Add categories attribute to core/latest-posts block. | |
* | |
* @param {*} settings | |
* @param {*} name | |
*/ | |
function latestPostsCategories( settings, name ) { | |
// Only modify latest posts block settings. | |
if ( name !== 'core/latest-posts' ) { | |
return settings; | |
} | |
settings.attributes.categories = { | |
"categories": { | |
"type": "array", | |
"items": { | |
"type": "object" | |
} | |
}, | |
}; | |
return settings; | |
} | |
wp.hooks.addFilter( | |
'blocks.registerBlockType', | |
'slug/latest-posts-categories', | |
latestPostsCategories | |
); | |
/** | |
* Add categories control to latest posts block. | |
*/ | |
const categoriesOption = createHigherOrderComponent( ( BlockEdit ) => { | |
return ( props ) => { | |
// Only add option to latest posts block. | |
if ( props.name !== 'core/latest-posts' ) { | |
return <BlockEdit { ...props } /> | |
} | |
const { setAttributes, attributes } = props; | |
const { | |
categories, | |
} = attributes; | |
return ( | |
<Fragment> | |
<BlockEdit { ...props } /> | |
<InspectorControls> | |
<PanelBody | |
title={ __( 'Categories', 'slug' ) } | |
> | |
// Categories control goes here. | |
</PanelBody> | |
</InspectorControls> | |
</Fragment> | |
); | |
}; | |
}, "withInspectorControl" ); | |
wp.hooks.addFilter( | |
'editor.BlockEdit', | |
'slug/categoriesOption', | |
categoriesOption | |
); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment