- Navigate to the ch6-book-review-user-submission directory of the WordPress plugin folder of your development installation.
- Open the file ch6-book-review-user-submission.php in a text editor.
- Add the following line of code to register a function that will intercept user-submitted book reviews:
add_action( 'template_redirect',
'ch6_brus_match_new_book_reviews' );
- Add the following block of code to implement the ch6_brus_match_new_book_reviews function:
function ch6_brus_match_new_book_reviews( $template ) {
if ( !empty( $_POST['ch6_brus_user_book_review'] ) ) {
ch6_brus_process_user_book_reviews();
} else {
return $template;
}
}
- Insert the following code to provide an implementation for the ch6_brus_process_user_book_reviews:
function ch6_brus_process_user_book_reviews() {
// Check that all required fields are present and non-empty
if ( wp_verify_nonce( $_POST['br_user_form'],
'add_review_form' ) &&
!empty( $_POST['book_title'] ) &&
!empty( $_POST['book_author'] ) &&
!empty( $_POST['book_review_text'] ) &&
!empty( $_POST['book_review_book_type'] ) &&
!empty( $_POST['book_review_rating'] ) ) {
// Create array with received data
$new_book_review_data = array(
'post_status' => 'publish',
'post_title' => $_POST['book_title'],
'post_type' => 'book_reviews',
'post_content' => $_POST['book_review_text'] );
// Insert new post in website database
// Store new post ID from return value in variable
$new_book_review_id =
wp_insert_post( $new_book_review_data );
// Store book author and rating
add_post_meta( $new_book_review_id, 'book_author',
wp_kses( $_POST['book_author'], array() ) );
add_post_meta( $new_book_review_id, 'book_rating',
(int) $_POST['book_review_rating'] );
// Set book type on post
if ( term_exists( $_POST['book_review_book_type'],
'book_reviews_book_type' ) ) {
wp_set_post_terms( $new_book_review_id,
$_POST['book_review_book_type'],
'book_reviews_book_type' );
}
// Redirect browser to book review submission page
$redirect_address =
( empty( $_POST['_wp_http_referer'] ) ? site_url() :
$_POST['_wp_http_referer'] );
wp_redirect( add_query_arg( 'add_review_message', '1',
$redirect_address ) );
exit;
} else {
// Display message if any required fields are missing
$abort_message = 'Some fields were left empty. Please ';
$abort_message .= 'go back and complete the form.';
wp_die( $abort_message );
exit;
}
}
- In the original ch6_brus_book_review_form function, add the following code after the wp_nonce_field function call:
<?php if ( isset( $_GET['add_review_message'] )
&& $_GET['add_review_message'] == 1 ) { ?>
<div style="margin: 8px;border: 1px solid #ddd;
background-color: #ff0;">
Thank for your submission!
</div>
<?php } ?>
<!-- Post variable to indicate user-submitted items -->
<input type="hidden" name="ch6_brus_user_book_review" value="1" />
- Save and close the plugin file.
- Go back to the book review submission form and submit a review to send all the fields to the newly created processing function. After processing the new content, the script will return to the form, which will display a confirmation message.