WordPress Site A has an API, which is being used by another system/application (B). B is struggling to handle/controle/sanitize the posts being sent to the API, and often cause creation of duplicates. B is really struggling to solve this in their end, so I was wondering if I can add a validation-method to the endpoint in my end.
The problem is that it needs to be a validation on a create-endpoint.
Example
This is how I imagines it would look on an update endpoint (not tested):
I'm calling the endpoint: https://example.org/wp-json/wp/v2/foobar/100007?title=Test Title&meta[custom_var]=10
add_action( 'rest_api_init', function(){
register_rest_route( 'wp/v2', '/foobar/(?P<id>\d+)', [
'methods' => 'POST',
'args' => [
'custom_var' => [
'validate_callback' => function( $param, $request, $key ){
if( $param > 5 ){
return true
}
return false;
},
],
],
] );
} );
But I can't find anything in the rest documentation about validating the create endpoint.
I imagined it being something like this:
add_action( 'rest_api_init', function(){
register_rest_route( 'wp/v2', '/foobar/', [
'methods' => 'POST',
'validate_method' => function( ... ),
...
...
...
] );
} );
... But that doesn't work.
I also considered doing the callback:
add_action( 'rest_api_init', function(){
register_rest_route( 'wp/v2', '/foobar/', [
'methods' => 'POST',
'callback' => function( ... ),
...
...
...
] );
} );
But that callback fires after the post is created (I assumed).
'callback' => function () { if ( some condition ) { create post } else { return existing post or something else } }
. Or am I not understanding the question?