hooks - Validate custom fields before save using WordPress Rest API

admin2025-06-05  3

I want to add custom validation to post(custom post type) before it save into the WordPress. I am using the Rest API to insert the post to custom post type. What I need is the fields which I created using ACF(Advance Custom Fields) should be validated before it actually save into the WordPress Backend. I tried to find the hook action for before save but no luck so far. Quick help will be appreciate.

I want to add custom validation to post(custom post type) before it save into the WordPress. I am using the Rest API to insert the post to custom post type. What I need is the fields which I created using ACF(Advance Custom Fields) should be validated before it actually save into the WordPress Backend. I tried to find the hook action for before save but no luck so far. Quick help will be appreciate.

Share Improve this question asked Dec 12, 2018 at 13:06 Jenil KananiJenil Kanani 1215 bronze badges 12
  • Thanks for the quick answer. But When I call the API using the POST method for storing the data into custom post type. It's not validates the fields which I checked as required in ACF. – Jenil Kanani Commented Dec 12, 2018 at 13:11
  • Hi @vikrantzilpe The above functions not worked for me for custom post type. Do you have any work around here? – Jenil Kanani Commented Dec 12, 2018 at 13:16
  • Thanks for look into this but above link is only guide me how to create custom post type. – Jenil Kanani Commented Dec 12, 2018 at 13:41
  • @vikrantzilpe Do you find any hooks actions that runs before saving the posts where I can validate each fields that passed into rest api? – Jenil Kanani Commented Dec 12, 2018 at 13:42
  • add_action('acf/validate_save_post', 'my_acf_validate_save_post', 10, 0); – vikrant zilpe Commented Dec 12, 2018 at 13:44
 |  Show 7 more comments

1 Answer 1

Reset to default 1

By searching a lot to API documentation, I found the solution to my problem finally:

function my_rest_prepare_post(  $prepared_post, $request ) {

...

}
add_filter( 'rest_pre_insert_posttype', 'my_rest_prepare_post', 10, 3 );
转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1749105802a316410.html

最新回复(0)