If your option is enabled in the REST API, e.g. by using register_setting()
with 'show_in_rest' => true
, you can use wp.data.select( 'core' ).getSite()
to retrieve the value of your option, and to update the option, you can use wp.data.dispatch( 'core' ).saveSite()
.
So for example in your case:
-
To get the option value:
wp.data.select( 'core' ).getSite()?.autoupdate
-
To update the option (to
true
):wp.data.dispatch( 'core' ).saveSite( { autoupdate: true } )
Note: This function will make an AJAX request to the Site Settings endpoint, so you will need to wait a while before
getSite()
returns the updated options.
Example using useSelect
and useDispatch
in the edit
function:
// In the `edit` function:
// Get the current option value.
const autoupdate = useSelect(
( select ) => select( 'core' ).getSite()?.autoupdate,
[]
);
// Extract the function which saves the new option value.
const { saveSite } = useDispatch( 'core' );
// Save the new option value.
function saveAutoupdate( autoupdate ) {
saveSite( { autoupdate } );
}
return (
<div { ...useBlockProps() }>
<CheckboxControl
label="Auto-Update"
help="Additional help text"
checked={ !! autoupdate }
onChange={ saveAutoupdate }
/>
</div>
);