Skip to content

Instantly share code, notes, and snippets.

@goiblas
Created August 4, 2019 08:52
Show Gist options
  • Save goiblas/0171ae1454269d1843884903b5bb1986 to your computer and use it in GitHub Desktop.
Save goiblas/0171ae1454269d1843884903b5bb1986 to your computer and use it in GitHub Desktop.
Block restrict content
const { registerBlockType } = wp.blocks;
const { Fragment } = wp.element;
const { InspectorControls } = wp.editor;
const { InnerBlocks } = wp.editor;
const { PanelBody, SelectControl } = wp.components;
registerBlockType( 'block-restrict-content/block-restrict-content', {
title: 'Restrict Content',
icon: 'lock',
category: 'layout',
edit: ({ attributes, setAttributes } ) => {
const { rolesSelected, roles } = attributes;
const template = [ 'core/paragraph', { placeholder: 'Enter the content...' } ];
return (
<Fragment>
<InspectorControls>
<PanelBody>
<SelectControl
multiple
label='Roles:'
value={ rolesSelected }
onChange={ ( rolesSelected ) => {
setAttributes( { rolesSelected } )
} }
options={ roles.map( role => {
return {
value: role,
label: role
}
} ) }
/>
</PanelBody>
</InspectorControls>
<div style={ { padding: "20px 20px 2px", backgroundColor: "#f4f4f4" }}>
<div style={{ textTransform: "uppercase", marginBottom: "10px", fontSize: "12px"}}>
Available to { rolesSelected.join(", ") }
</div>
<InnerBlocks template={ [template] } />
</div>
</Fragment>
)
},
save: () => <InnerBlocks.Content />
} );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment