Skip to content

Instantly share code, notes, and snippets.

@misfist
Last active November 5, 2020 20:49
Show Gist options
  • Save misfist/0eb0ac42c5d85df663402f981545fcc9 to your computer and use it in GitHub Desktop.
Save misfist/0eb0ac42c5d85df663402f981545fcc9 to your computer and use it in GitHub Desktop.
Gutenberg - Custom Taxonomy Selector - Select Control
const {
SelectControl,
} = wp.components;
const taxonomy = 'category';
function customTaxonomySelectControlSelector( OriginalComponent ) {
return function(props) {
if ( props.slug === 'my_taxonomy' ) {
if ( !window.HierarchicalTermSelectSelector ) {
window.HierarchicalTermSelectSelector = class HierarchicalTermSelectSelector extends OriginalComponent {
// Return only the selected term ID
onChange(value) {
console.info(event);
const {
onUpdateTerms,
taxonomy
} = this.props;
const termId = parseInt( value, 10 );
onUpdateTerms([termId], taxonomy.rest_base);
}
// Copied from HierarchicalTermSelector, changed input type to radio
renderTerms( renderedTerms ) {
const {
terms = []
} = this.props;
return ( < SelectControl label = {
__( 'Select Category' )
}
value = {
terms[0]
} // e.g: value = [ 'a', 'c' ]
onChange = {
this.onChange
}
options = {
renderedTerms.map((term) => {
return {
value: term.id,
label: term.name
};
})
}
/>)
}
};
}
return <window.HierarchicalTermSelectSelector { ...props
}
/>;
}
return <OriginalComponent { ...props
}
/>;
};
}
wp.hooks.addFilter( 'editor.PostTaxonomyType', 'core-functionality', customTaxonomySelectControlSelector );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment