After submitting the book review form data to the page containing the book review submission form in the previous recipe, the first few steps of this recipe will be to assign a function to the template_redirect action hook to allow us to capture new book review content. This hook function is executed early in the WordPress processing sequence. If found, we will call the processing function that is defined in the rest of the recipe.
The first thing that is done in our processing function is to check whether the proper hidden data field is found as part of the post data, using the wp_verify_nonce function. If it is not present, indicating that someone may be trying to post data to the website without having visited the frontend form, it will display an error message.
When we are sure that our data storage script is being called legitimately, we will continue processing the actual data by first checking to see whether all the fields are present and are not empty. If that is not the case, we will display an error message, asking the user to go back and complete the form using the wp_die function.
If all the fields have been received correctly, the recipe continues to process the incoming data by preparing an array of information that includes the newly submitted title and review text, along with a post status and the book_reviews post type name. The resulting array is sent to the wp_insert_post function to store the information. As we can see, wp_insert_post only requires a single parameter that is fulfilled using the array that we just created. While we only define four elements of that array, many more are available, which can be seen by consulting the WordPress Codex (https://developer.wordpress.org/reference/functions/wp_insert_post/).
Now, calling wp_insert_post only takes care of storing some key data elements that belong in the post data. We must follow up this code with calls to update_post_meta and wp_set_post_terms to store the remaining user information to the website database.
Once all the information is stored, we use a combination of the wp_redirect and add_query_arg functions to send the user back to the page where he submitted a book review, while making sure that only one instance of the add_review_message variable is in the target address.
Last but not least, this recipe makes a small modification to the code that rendered the book review form to add a confirmation message that is shown to visitors when information is accepted by the plugin.