Is the 404 page title, in your WordPress website not what you want? Do you want to customize it? Do you see the page URL in place of the title in the web browser tab? I do not have a page xyz
on my testing website on the local host. Therefore, below is the 404 title:
In this situation, if you right-click somewhere on the 404 page and select view source, you see the empty title tag: <title></title>
.
There is no longer a 404.php file in block themes to edit directly, and the 404.html template file cannot change the title. If you want to change the title, read the rest of this blog post.
Change the WordPress 404 Page Title
First, you need to add the following code to the functions.php file of your child theme. Always back up your theme files before making any modifications, and be cautious when editing code directly.
If you do not have a child theme or do not know how to edit the functions.php file, these articles are helpful: How to Create a WordPress Child Theme and How to Edit functions.php on WordPress Themes.
/* Change 404 page title text */
function alvand_filter_wp_title($title)
{
if (is_404()) {
$title = 'Page Not Found | Alvand';
}
return $title;
}
add_filter('pre_get_document_title', 'alvand_filter_wp_title', 10);
Second, instead of Page Not Found | Alvand
in the above code, enter your desired page title text suitable for your website. Also, clear the cache to ensure changes are reflected immediately if you’re using a caching plugin or server-side caching.
In my case, consequently, you will see this in your browser:
Know More About This Code
- First, I define a function
alvand_filter_wp_title
that takes the current title as input and returns a modified title if the current page is a 404 error page. - Next, Inside the function, I check if the current page is a 404 error page using the
is_404()
function. - After that, If it’s a 404 page, I set the
$title
variable to my custom title, ‘Page Not Found | Alvand
‘. - Finally, I use the
add_filter()
function to hook my custom function into thepre_get_document_title
filter, which allows us to modify the page title before it’s displayed.
Conclusion
In conclusion, following the above, you can easily change the title of your 404 error page in WordPress. In addition, if you want to customize your WP website more, read this article: How to Set a Default Featured Image in WordPress
Leave a Reply