How to Change the 404 Page Title in WordPress?

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:

WordPress 404 page web browser tab without a nice 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:

WordPress 404 page web browser tab with a suitable title

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 the pre_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

Published on:

Updated on:

Author:

Rate this post and help us improve our content to better serve you.

Help us spread the word! Click the share buttons below to share this post with your followers.

We’d love to hear your thoughts on this topic. Feel free to leave a comment below.

Responses

  1. Zee Avatar
    Zee

    Thank you! I searched deep and wide for this solution.

    1. Mohammad Ahmadian Avatar
      Mohammad Ahmadian

      You’re welcome. It’s my pleasure that you found the solution.

Leave a Reply

Your email address will not be published. Required fields are marked *

fifteen + 12 =