$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>hooks - Validate custom fields before save using WordPress Rest API|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

hooks - Validate custom fields before save using WordPress Rest API

matteradmin9PV0评论

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 );
Post a comment

comment list (0)

  1. No comments so far