- Navigate to the ch4-book-reviews folder of the WordPress plugin directory of your development installation.
- Open the ch4-book-reviews.php file in a code editor.
- Add the following line of code after the existing functions to register a function to be called when WordPress prepares the text to be displayed in the browser's title bar:
add_filter( 'document_title_parts',
'ch4_br_format_book_review_title' );
- Add the following code section to provide an implementation for the ch4_br_format_book_review_title function:
function ch4_br_format_book_review_title( $the_title ) {
if ( 'book_reviews' == get_post_type() && is_single() ) {
$book_author = esc_html( get_post_meta( get_the_ID(),
'book_author', true ) );
if ( !empty( $book_author ) ) {
$the_title['title'] .= ' by ' . $book_author;
}
}
return $the_title;
}
- Save and close the plugin file.
- Visit a Book Review page. You will see that the book's author is now displayed after the name in the title: