$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'); ?>How to handle new post from API request?|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)

How to handle new post from API request?

matteradmin9PV0评论

I feel silly for even asking this, as it seems like it should be very straightforward but I'm having a hard time finding this information.

Goal: Make a POST request to the WordPress API with new post information. Take the new post information, validate it, and if information is valid and user has privileges add the post to the database.

I assume that I would need a plugin to expose a new route and have it handle all the requests to that route. I have a headless setup with nextjs and I need the frontend user to be able to add a post. I can figure out the frontend half, it's the backend half I'm having issues with.

If there's anymore information you need please let me know I'll edit my question to make it more clear. Thank you for taking the time to read this!

I feel silly for even asking this, as it seems like it should be very straightforward but I'm having a hard time finding this information.

Goal: Make a POST request to the WordPress API with new post information. Take the new post information, validate it, and if information is valid and user has privileges add the post to the database.

I assume that I would need a plugin to expose a new route and have it handle all the requests to that route. I have a headless setup with nextjs and I need the frontend user to be able to add a post. I can figure out the frontend half, it's the backend half I'm having issues with.

If there's anymore information you need please let me know I'll edit my question to make it more clear. Thank you for taking the time to read this!

Share Improve this question asked Oct 24, 2018 at 19:24 kalm42kalm42 1052 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 1

Goal: Make a POST request to the WordPress API

Indeed, make your POST request to example/wp-json/wp/v2/posts. Make sure pretty permalinks are turned on!

with new post information.

This would be the information being POST'd, and should take the same format as when going into wp_insert_post

Take the new post information, validate it

WP will do this automatically for its own fields. If you wished to register a new endpoint, you could declare all the possible fields, wether they're required or optional, and sanitising and validation callbacks. The API will take care of returning any error objects back to the client in the appropriate format with the correct HTTP code

and if information is valid and user has privileges add the post to the database.

The REST API already checks this, but if you add your own endpoint you can specify a capability, or even a more in depth callback

I assume that I would need a plugin to expose a new route and have it handle all the requests to that route.

If you desire a custom endpoint then yes, you will need to use register_rest_route and provide the needed information.

Documentation on Adding Custom Endpoints

I have a headless setup with nextjs and I need the frontend user to be able to add a post. I can figure out the frontend half, it's the backend half I'm having issues with.

At this point it's a standard REST API, with 2 differences:

  • WP will want the user to be logged in with cookies
  • WP will want a valid nonce

If you enqueue the wpapi script, it'll insert the URL to the REST API, as well as a valid nonce you can use. Assuming this is all on the same domain. You can then use the wpapi library to make the requests, or fetch or jQuery().post etc

If you want to interact with a remote WP install on a different domain/server though, you'll need a plugin to implement authentication, such as OAuth1/2/etc

Articles related to this article

Post a comment

comment list (0)

  1. No comments so far