How to set custom page title for homepage WordPress

Hello friend, welcome to my blog post how to set custom page title for homepage WordPress. When we use a wp_title() function inside title tag and set a custom page template as a front page then we will see a blank page title in your page source code and see your domain name like www.example.com into your page title. So you need to set a custom page title for the homepage. So we can use WordPress filter hook to set page title. The wp_title filter is used to filter the title of the page (called with wp_title()). This filters the text appearing in the HTML title tag (sometimes called the “title tag” or “meta title”), not the post, page, or category title.

Set custom page title for homepage WordPress




A plugin (or theme) can register as a content filter with the below code:

add_filter( 'wp_title', 'filter_function_name', 10, 2 );

Where ‘filter_function_name’ is the function WordPress should call when the content is being retrieved. Note that the filter function must return the content after it is finished processing, or the title will be blank and other plugins also filtering the content may generate errors.

You can also check How to enqueue inline scripts in header WordPress

filter_function_name should be unique function name. It cannot match any other function name already declared.

Set custom page title for homepage WordPress

Copy the code and paste it inside your theme functions.php:

add_filter( 'wp_title', 'custom_wp_title_for_home' );
 
/**
 * Customize the title for the home page, if one is not set.
 *
 * @param string $title The original title.
 * @return string The title to use.
 */
function custom_wp_title_for_home( $title )
{
  if ( empty( $title ) && ( is_home() || is_front_page() ) ) {
    $title = get_bloginfo( 'name' ) . ' | ' . get_bloginfo( 'description' );  // get_bloginfo('name) will returns the "Site Title" set in Settings > General and get_bloginfo( 'description' ) will  returns the "Tagline" set in Settings > General.
  }
  return $title;
}

In header.php:

<title><?php wp_title('|', true, 'right'); ?></title>

Go to your website home page and refresh the page. Now you will see the changes.

Now you know how to set custom page title for homepage into your WordPress website. If you have any question or suggestion please comment below. If you like my post then please share it with your friends. Thank you.

Spread the love
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •