Create events from front end and send it directly to WP REST API

Hello, dear community. Currently, I’m building a website that on one page shows events, and one of the features I should implement is to allow users to create events. . I’ve been reading some threads in the forum, and I understand that I need authentication to post on the REST API.

1.- First of all, I understand that I need to install thisJWT Auth plugin and make some configurations in some php files.

2.- Then, according to this thread and the useful snippet provided by @vscopise at the end of the thread, I should make the login/ authentication process.

3.- After that, I guess I have to do a new request through a form sending the data of the event I want to POST. If I understand correctly I should do something similar to the snippet of authentication. Or … Should I do the authentication and POST the event at the same time?

4.- The events posts are organized inside a Custom Post Type with the ‘allevents’ endpoint, and also it has many advanced custom fields made it, with the help of ACF plugin. Do you think is gonna be a problem to make a POST request of events that includes advanced custom fields (acf)? If so, How should I face this case?

I hope I explained the case properly. Thank you so much in advance for all the help.

Assuming someone needs to be logged in before he/she can post an event, the authentication is already done and you only have to send the the auth key with the POST. And in the endpoint handler at the WP side you need to check if the request has the auth key and its valid, before handling the POST data.

Alternatively, when you don’t have regular users and handle it like comments without an account, you can simply POST it without authentication. However in that case I wouldn’t automate the process and manually check each posted event before publishing.

If you create a custom endpoint it doesn’t matter which data gets sent and handled. It will only require a bit of PHP to make this work:

add_action('rest_api_init', function () {
   register_rest_route('myplugin/v1', '/events', array(
      'methods' => 'POST',
      'callback' => function(\WP_REST_Request $request) {
         $params = $request->get_json_params();

         // go through whatever you've sent
         // - validate and sanitize data
         // - create new event
         // - add ACF content
         // - return 20x status when success, 40x when error(s)
      },
   ));
});
2 Likes

Thank you @Johan for your reply, so , Do you recommend to build a custom endpoint to handle this acf fields? otherwise I won’t be able to post an event?. If so where should I put this function?, in functions.php files of the default theme? Sorry I’m not well versed in wordpress backend or php.

I will try this solution as soon as possible.

Thanks again.