I've created a custom meta box, but i can't update that through api rest. This is my code. The var $_POST in a function save_custom_meta_box is always empty when I send a request.
Create meta box
add_action( 'add_meta_boxes', 'adding_custom_meta_boxes', 10, 2 );
if (!function_exists('adding_custom_meta_boxes')) {
function adding_custom_meta_boxes( $post_type, $post ) {
add_meta_box(
"demo-meta-box",
"Custom Meta Box",
"custom_meta_box_markup",
"post",
"side",
"high",
null
);
}
}
Storing Meta Data
add_action("save_post", "save_custom_meta_box", 10, 3);
function save_custom_meta_box($post_id, $post, $update)
{
if(!current_user_can("edit_post", $post_id)){
return $post_id;
}
if($post->post_type != "post"){
return $post_id;
}
$meta_box_checkbox_value = "";
if(isset($_POST["meta-box-checkbox"]))
{
$meta_box_checkbox_value = $_POST["meta-box-checkbox"];
}
update_post_meta($post_id, "meta-box-checkbox", $meta_box_checkbox_value);
}
SOLUTION: Use action rest_after_insert_post.
add_action('rest_after_insert_post', 'wp_kama_rest_after_insert_post_type_action', 10, 3);
function wp_kama_rest_after_insert_post_type_action( $post, $request, $creating ) {
if ($creating) {
return;
}
$body = json_decode($request->get_body());
if($body->metaBoxCheckbox)
{
update_post_meta($post->ID, "meta-box-checkbox", $body->metaBoxCheckbox);
}
return;
}