I registered a boolean meta field called _meta_restrict
in a custom post type:
PHP:
function register_meta_fields() {
register_post_meta( 'custom_post_type', '_meta_restrict', array(
'show_in_rest' => true,
'single' => true,
'type' => 'boolean',
'auth_callback' => function() {
return current_user_can( 'edit_posts' );
}
) );
}
add_action( 'init', 'register_meta_fields' );
JS:
let RestrictControl = ({ restrict, onUpdateRestrict }) => (
<ToggleControl
label={ 'Restrict access?' }
help={ restrict ? 'Access is restricted.' : 'Access is not restricted.' }
checked={ restrict }
onChange={ restrict => onUpdateRestrict( restrict ) }
/>
);
RestrictControl = compose( [
withSelect( ( select ) => {
return {
restrict: select( 'core/editor' ).getEditedPostAttribute( 'meta' )['_meta_restrict']
};
} ),
withDispatch( ( dispatch ) => {
return {
onUpdateRestrict: ( value ) => {
dispatch( 'core/editor' ).editPost({ meta: { _meta_restrict: value } })
}
}
} ),
] )( RestrictControl );
I added this meta field using a ToggleControl
to the PluginDocumentSettingPanel
and I can toggle it on and off, but when I save changes, the _meta_restrict
key is stored in the database with an empty value.
What am I doing wrong?
Thanks in advance