Skip to content

Instantly share code, notes, and snippets.

@igmoweb
Last active June 3, 2020 08:59
Show Gist options
  • Save igmoweb/afa638b28ce3c9577e5e79879b1d87de to your computer and use it in GitHub Desktop.
Save igmoweb/afa638b28ce3c9577e5e79879b1d87de to your computer and use it in GitHub Desktop.
<?php
register_post_meta( 'page', 'sidebar_plugin_meta_block_field', [
'show_in_rest' => [
'prepare_callback' => __NAMESPACE__ . '\\prevent_meta_exposition'
],
'single' => true,
'type' => 'string',
'sanitize_callback' => 'sanitize_text_field',
] );
/**
* Keep a meta key private in the REST API, only to users that can edit the current post.
*
* @param mixed $value
* @param \WP_REST_Request $request
*
* @return string
*/
function prevent_meta_exposition( $value, $request ) {
if ( ! current_user_can( 'edit_post', $request->get_param( 'id' ) ) ) {
return '';
}
return $value;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment