wp_title

Home » Hooks » wp_title

The WordPress hook wp_title provides a way to customize the title of a WordPress site or page. This hook allows developers to add or modify content that appears in the title tag of a page. The title tag is one of the most important meta tags in the HTML head section of a webpage, as it tells the search engines and users what the content of a page is about.

Developers can use the wp_title hook to add custom text, modify the default title structure, or add dynamic content to the title tag. The wp_title hook passes two parameters – the title text and a separator – which can be modified by the developer to create a custom title.

Here is an example usage code for wp_title hook:

function custom_wp_title( $title, $separator ) {
    if ( is_front_page() ) { // Change the title tag for the front page
        $title = get_bloginfo( 'name' ) . ' | ' . get_bloginfo( 'description' );
    } elseif ( is_category() ) { // Change the title tag for category pages
        $title = single_cat_title( '', false ) . " $separator " . get_bloginfo( 'name' );
    } elseif ( is_page() ) { // Change the title tag for individual pages
        $title = get_the_title() . " $separator " . get_bloginfo( 'name' );
    }
    return $title;
}
add_filter( 'wp_title', 'custom_wp_title', 10, 2 );

In this example, we’ve created a custom_wp_title function that modifies the title text and separator based on the type of page being displayed. The function first checks if the page is the front page, and if so, adds the site name and description to the title tag. If the page is a category archive page, the function adds the category name to the title tag. Finally, if the page is an individual page, the function adds the page title to the title tag.

This is just one example of how the wp_title hook can be used to customize the title tag in WordPress. There are many different ways to use this hook to add dynamic content or modify the default title structure depending on your needs.

Learn More on WordPress.org

WordPress snippets using the wp_title hook

Register an account to save your snippets or go Pro to get more features.