To publish a post using the WordPress Rest API, we first need to use theUser Authentication via WordPress Rest APIThen we can publish the article through the Rest API, if we publish the default article, it is very simple, I believe that a little Rest API development experience of friends can be dealt with, if you need to submit WordPress articles outside the other fields, things have become complicated, because the WordPress Rest API, the article interface accepts the data is limited to the default fields in the wp_posts data table, if we need to submit custom fields and attach them to the currently submitted post, we need to do some more work.
Let's take a look at the form below, where "Title of your story" and "Submission" are the title and body content of the article respectively, and all other fields are custom fields.
Format of data submitted by the front-end
We are using React to develop the above form, if you use jQuery, PHP, Python or other languages, the process is similar. First, we need to get the data that we need to send to the Rest API interface, the 'values' in the code below is the data we are going to ask for, where 'title' and 'content' are the default article data, and 'metadata' is the default article data. content' are the default post data, and 'metadata' is an array of custom field objects we added. The "submitPost" is the encapsulated method for submitting data to the Rest API.
// The data needed to publish the article
values.title = rawValues.title;
values.content = rawValues.content; values.metadata = [ rawValues.metadata; }
values.metadata = [
{'key' : 'name', 'value': rawValues.name}, {'key' : 'email', 'value': rawValues.name}, }
{'key' : 'topic', 'value': rawValues.topic}, {'key' : 'name', 'value': rawValues.name}, {'key' : 'email', 'value': rawValues.
{'key' : 'photo', 'value': rawValues.photo}, {'key' : 'photo', 'value': rawValues.
{'key' : 'agree', 'value': rawValues.agree}
];
const submission = this.props.submitPost(values, 'contribute');
When sending data, the "metadata" in the form is appended to the data we submit, along with the default Post data to the WordPress Rest API interface.
Methods of back-end processing
The default Rest API article interface doesn't handle this additional "metadata" data when it's submitted from the frontend, WordPress provides hooks for us to handle this additional data on our own. Let's take a look at the code below to see how to save the additional data submitted via the Rest API to a custom field in the post.
add_action( 'rest_api_init', function() {
// The 'post' post type in the next line can be any other custom post type; the 'metadata' is the metadata array submitted by our front-end
register_rest_field( 'post', 'metadata', array(
// The callback for displaying the data, where we can append the custom field data to the Json data returned by the Rest API article interface.
'get_callback' => function( $object ) {
return get_post_meta($object->ID);
},
// The data-saving callback, where we get the additional data submitted by the front-end and save it one by one to the custom field
'update_callback' => function( $meta, $post ) {
$postId = $post->ID;
foreach ($meta as $data) {
update_post_meta($postId, $data['key'], $data['value']);
}
return true.
},
));
}).
With the code above, we can then pass the WordPress Rest API Getting and saving custom fields for post types, with this feature, the flexibility of WordPress can be played out. It should be reminded that the above code will not create a custom field UI form in the WordPress post editing interface, if needed, we can create it through the Metabox API or some other plugins.