Skip to content

Instantly share code, notes, and snippets.

@florianbrinkmann
Created May 6, 2020 14:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save florianbrinkmann/10561fd9c4bcfe23ed43d442c6243f76 to your computer and use it in GitHub Desktop.
Save florianbrinkmann/10561fd9c4bcfe23ed43d442c6243f76 to your computer and use it in GitHub Desktop.
/**
* 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