Last active
June 3, 2020 08:59
-
-
Save igmoweb/afa638b28ce3c9577e5e79879b1d87de to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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