Change the Page Title Dynamically
by Sam
Page title is one of the considerations by the search engines to crawl with. Therefore, it is important to have a user-friendly page title.
In wordpress, you have different ways to set the page title.
Option 1
add_filter('wp_title', set_page_title());
function set_page_title() {
$title = 'Page Title - '.get_bloginfo('name');
return $title;
}
Note that the above code works only on codes below wordpress 4.4 version. Check the link below.
Option 2
This solution only works on Main Page title but not changing the bloginfo name.
function custom_title($title_parts) {
$title_parts['title'] = "Page Title";
return $title_parts;
}
add_filter( 'document_title_parts', 'custom_title' );
You might also like to test the code below:
function custom_title($title) {
return "Page Title";
}
add_filter( 'pre_get_document_title', 'custom_title' );
If you have another way of changing the page title, don’t hesitate to comment below.