- 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.
- Insert the following line of code to register a function to be called back when new posts are submitted:
add_action( 'wp_insert_post', 'ch6_brus_send_email', 10, 2 );
- Insert the following block of code to implement the ch6_brus_send_email function:
function ch6_brus_send_email( $post_id, $post ) {
// Only send emails for user-submitted book reviews
if ( isset( $_POST['ch6_brus_user_book_review'] ) &&
'book_reviews' == $post->post_type ) {
$admin_mail = get_option( 'admin_email' );
$headers = 'Content-type: text/html';
$message = 'A user submitted a new book review to your ';
$message .= 'WordPress website database.<br /><br />';
$message .= 'Book Title: ' . $post->post_title ;
$message .= '<br /><a href="';
$message .= add_query_arg( array(
'post_status' => 'draft',
'post_type' => 'book_reviews' ),
admin_url( 'edit.php' ) );
$message .= '">Moderate new book reviews</a>';
$email_title = htmlspecialchars_decode( get_bloginfo(),
ENT_QUOTES ) . " - New Book Review Added: " .
htmlspecialchars( $_POST['book_title'] );
// Send email
wp_mail( $admin_mail, $email_title, $message, $headers );
}
}
- Save and close the plugin file.
- Go back to the book review submission form and submit a book review. An email will be sent to the address associated with the website administrator, containing some information from the new review: