RestAPI: cmb2 field not updated
-
I created a custom post type ps_immobilie that has a field _psi_geo_plz like that:
function add_geo_mb( $prefix, array $meta_boxes ) { $meta_boxes['geo_metabox'] = array( 'id' => 'geo_metabox', 'title' => __( 'Ort', 'cmb2' ), 'object_types' => array( 'ps_immobilie', ), // Post type 'context' => 'normal', 'priority' => 'high', 'show_names' => true, // Show field names on the left 'show_in_rest' => WP_REST_Server::ALLMETHODS, 'fields' => array( array( 'name' => __( 'PLZ', 'ps_immobilie' ), 'public' => true, 'id' => $prefix . 'geo_plz', 'type' => 'text', 'show_in_rest' => WP_REST_Server::ALLMETHODS, ), ), ); return $meta_boxes; }
I checked the CMB2 documentation on how to update the field with the Rest API an found the following URL:
https://<domain>/wp-json/cmb2/v1/boxes/geo_metabox/fields/_psi_geo_plz?object_type=ps_immobilie&object_id=13784&value=44444
When I send this request through Postman I get the following response (OK 200):
{ "type": "text", "name": "PLZ", "desc": "", "before": "", "after": "", "options": [], "text": [], "text_cb": "", "attributes": [], "protocols": null, "default": false, "default_cb": "", "classes": null, "classes_cb": "", "select_all_button": true, "multiple": false, "repeatable": false, "inline": false, "on_front": true, "show_names": true, "save_field": true, "date_format": "m\\/d\\/Y", "time_format": "h:i A", "description": "", "preview_size": [ 50, 50 ], "render_row_cb": "CMB2_Field::render_field_callback", "display_cb": "CMB2_Field::display_value_callback", "label": "CMB2_Field::label", "column": false, "js_dependencies": [], "public": true, "id": "_psi_geo_plz", "_id": "_psi_geo_plz", "_name": "_psi_geo_plz", "has_supporting_data": false, "update_field_value_permissions_check_cb": true, "value_updated": false, "value": "", "_links": { "self": [ { "href": "https://immoloader.powermaklersite.de/wp-json/cmb2/v1/boxes/geo_metabox/fields/_psi_geo_plz?object_id=13784&object_type=ps_immobilie" } ], "collection": [ { "href": "https://immoloader.powermaklersite.de/wp-json/cmb2/v1/boxes/geo_metabox/fields?object_id=13784&object_type=ps_immobilie" } ], "up": [ { "embeddable": true, "href": "https://immoloader.powermaklersite.de/wp-json/cmb2/v1/boxes/geo_metabox?object_id=13784&object_type=ps_immobilie" } ] }, "_embedded": { "up": [ { "id": "geo_metabox", "title": "Ort", "object_types": [ "ps_immobilie" ], "context": "normal", "priority": "high", "show_names": true, "show_on": [], "cmb_styles": true, "enqueue_js": true, "hookup": true, "save_fields": true, "closed": false, "taxonomies": [], "new_user_section": "add-new-user", "new_term_section": true, "show_in_rest": "GET, POST, PUT, PATCH, DELETE", "classes": null, "classes_cb": "", "remove_box_wrap": false, "mb_callback_args": null, "message_cb": "", "option_key": "", "parent_slug": "", "capability": "manage_options", "icon_url": "", "position": null, "admin_menu_hook": "admin_menu", "display_cb": false, "save_button": "", "disable_settings_errors": false, "tab_group": "", "update_field_value_permissions_check_cb": true, "get_field_permissions_check_cb": true, "delete_field_value_permissions_check_cb": true, "get_box_permissions_check_cb": true, } ] } }
The problem is that the field _psi_geo_plz is unchanged and blank. I would have expected it to contain 44444.
In my functions.php I set the following:
function custom_field_get_post_meta_cb($object, $field_name, $request){ error_log('HEUREKA - Get!'); $metas = get_post_custom($post_id); foreach($metas as $key => $value) { if(sizeof($value) == 1) { $metas[$key] = $value[0]; } } return $metas; } function custom_field_update_post_meta_cb($value, $object, $field_name){ error_log('HEUREKA - Update!'); error_log( print_r( $value, 1 ) ); return update_post_meta($object['id'], $field_name, $value); } add_action('rest_api_init', function(){ register_rest_field('ps_immobilie', '_psi_geo_plz', array( 'get_callback' => 'custom_field_get_post_meta_cb', 'update_callback' => 'custom_field_update_post_meta_cb') ); });
The function custom_field_update_post_meta_cb is not called at all. Maybe this is why the field does not get updated. But if so, what do I need to do in order to make it work?
Any help is very much appreciated!
Regards,
Dirk.
Viewing 10 replies - 1 through 10 (of 10 total)
Viewing 10 replies - 1 through 10 (of 10 total)
- The topic ‘RestAPI: cmb2 field not updated’ is closed to new replies.