wpmetabox / mb-rest-api Goto Github PK
View Code? Open in Web Editor NEWAdd custom fields to WordPress REST API
Add custom fields to WordPress REST API
Currently, there is no way to fetch the data for the MB Settings Pages via the REST APIs. I came across this old support ticket which states that the settings values are not sent to the REST API via MB REST API.
I'd like to know if there is a way through which we can get the data of the MB Settings Pages via REST APIs.
I purchased "MB USER META" and installed well.
I setup the "MB USER META Fields" like below.
(I can see the proper fields in Profile Page(admin) and when I modify data in DB, it looks well in the admin page)
but update user meta data with rest-api, this is not work.
for example.
this "POST Method Request" is not work.
PS.
I tested "mb-rest-api" with other type (like post) this was worked.
only with "user" wasn't work.
I checked so many times "post method" "application/json" type in header... etc...
Any idea how to filter the fields?
I'm trying to use filter[meta_query][0][key]=post_id&filter[meta_query][0][value]=77 but it does not apply any filter.
I'm using the rest-filter-master plugin
Thanks!
Because WP lacking any better classification on datas (no matter it is legacy implementation) then all API metabox response fields are not described enought.
All fields values all called with https://developer.wordpress.org/reference/functions/get_metadata/ and it has very unhappy implementation for 2 main situations when somebody else want to work with data:
Both situations should be fixed easy:
adding normalization function
protected function has_array_value($array)
{
return !empty(array_filter(array_values($array), function ($value) {
return is_array($value);
}));
}
protected function has_string_key($array)
{
return !empty(array_filter(array_keys($array), function ($key) {
return is_string($key);
}));
}
protected function normalize_value($field, $value)
{
if ($value == '') {
return null;
}
if (is_array($value)) {
if (empty($value)) {
return [];
} elseif ($this->has_array_value($value) || $this->has_string_key($value)) {
return $value;
} else {
return array_map([$this, 'sanitize_value'], $field, $value);
}
}
if (in_array($field['type'], $this->numeric_fields)) {
return numfmt_parse($field);
}
return $value;
}
numeric fields are all association fields, media fields, and number fields
$this->numeric_fields = array_merge($this->media_fields, ['number', 'post', 'taxonomy']);
and then just wrap it on
mb-rest-api/class-mb-rest-api.php
Line 272 in 78ffdf2
$values[ $field['id'] ] = $this->normalize_value($field, $field_value);
Results are:
Hi.
The Custom Post Types are now returning an empty array in the meta_box key API response.
I have updated to WordPress 5.0.1 today, and I have noticed about this after that. I really don't know if is something related to WP version.
The field type is date.
I already have updated the plugins too, but no success.
There's something that I have lost?
When I try to post and save a CPT with a custom field managed with Meta Box I get this error.
FastCGI sent in stderr: "PHP message: PHP Fatal error: Uncaught Error: Call to undefined method RWMB_Field::process_value() in /var/www/html/wp-content/plugins/mb-rest-api/class-mb-rest-api.php:221 web_1 | Stack trace: web_1 | #0 /var/www/html/wp-content/plugins/mb-rest-api/class-mb-rest-api.php(113): MB_Rest_API->update_value(Array, 1, 82818) web_1 | #1 /var/www/html/wp-includes/rest-api/endpoints/class-wp-rest-controller.php(461): MB_Rest_API->update_post_meta(Array, Object(WP_Post), 'meta_box', Object(WP_REST_Request), 'familygo_offert...') web_1 | #2 /var/www/html/wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php(659): WP_REST_Controller->update_additional_fields_for_object(Object(WP_Post), Object(WP_REST_Request)) web_1 | #3 /var/www/html/wp-includes/rest-api/class-wp-rest-server.php(1050): WP_REST_Posts_Controller->create_item(Object(WP_REST_Request)) web_1 | #4 /var/www/html/wp-includes/rest-api/class-wp-rest-server.php(376): WP_REST_Server->dispatch(Object(WP_REST_Request)) web_1 | #5 /var/www/html/wp-includes/rest-api.php(339): WP_REST_Server->serve_request('/wp/v2/offerta') web_1 | #6 /var/www/html/w in /var/www/html/wp-content/plugins/mb-rest-api/class-mb-rest-api.php on line 221
WordPress 5.5.3
MB Rest API 1.4.0
Meta Box 4.18.4
PHP 7.4
One challenge we have is restricting user access to the metadata......
I'm toying with the idea of adding an entry to each metadata defn containing a set of required capabilities (maybe an entry for read, entry for write), and a check with 'current_user_can' against these for data read and data write.
Any thoughts/plans on this?
line 84 complains that json_decode is being passed an array rather than a json string.
A quick debug showed this. Maybe they changed something in WP 4.7.3?
Also, if any of the meta-box fields is an array or an object, then strip_tags() complains, and the content of the array/object is destroyed on POST. so either way, writing meta-box data this way is currently broken.
When a custom taxonomy is created via POST request to rest api, the meta box fields are not saved on said request.
e.g. create a new custom taxonomy called Article tag. When registering the taxonomy make sure the rest api properties are set:
'show_in_rest' => true,
'rest_base' => 'video-tags',
'rest_controller_class' => 'WP_REST_Terms_Controller',
Register a simple meta box text field with your custom taxonomy.
array(
'id' => 'video_id',
'name' => esc_html__( 'Video ID', 'digg-core' ),
'type' => 'text',
)
A subsequent POST request to /wp-json/wp/v2/article-tags will result in a new tag being created with the parameter video_id value not set. The parameters passed in are:
"title": "A test video tag",
"meta_box": "{
"video_id": "12345"
}"
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.