I will use the wordperss REST API to submit a form that is part of SPA webapp made using vue. I have registered a custom post type to hold the submitted data, specifically I want to register some custom fields that will hold the submitted form fields.
I'm not sure on how to proceed, but after I've registered the post type, I think that I need to register a route register_rest_route()
to manage the data when they are submitted using a POST request, but how I save them? will the update_post_meta()
create the meta fields I want and wp_insert_post()
save then the custom post type?
UPDATE
The code I'm using to register the endpoint is the following:
function resgister_custom_endpoint(){
register_rest_route('mytheme/v1', 'p', array(
'method' => 'POST',
'callback' => 'add_new_place'
));
}
add_action('rest_api_init', 'register_custom_endpoint');
function add_new_place($request){
// save meta and post code
}
The meta fields I want to store with the custom post type I've created to manage the form are:
$_POST['fname'], $_POST['lname'], $_POST['email'], $_POST['place_name'], $_POST['place_type'], $_POST['place_address'], $_POST['place_city'], $_POST['place_zip_code'], $_POST['place_province']
They are all part of the form that is submitted from the user from the front-end.