5

My theme has support for title-tag add_theme_support('title-tag'), but I can't remove the wordpress description bloginfo('description') from <title> on Home page.

I'm trying using this filter with no success:

add_filter( 'wp_title', function ( $title, $sep ) {
    global $paged, $page;

    $title .= get_bloginfo( 'name' );

    if ( is_home() || is_front_page() )
        $title = "$title";

    return $title;
}, 10, 2 );

3 Answers 3

9

wp_get_document_title() has some interesting filters - pre_get_document_title and document_title_parts.

/**
 * Filter the parts of the document title.
 *
 * @since 4.4.0
 *
 * @param array $title {
 *     The document title parts.
 *
 *     @type string $title   Title of the viewed page.
 *     @type string $page    Optional. Page number if paginated.
 *     @type string $tagline Optional. Site description when on home page.
 *     @type string $site    Optional. Site title when not on home page.
 * }
 */
add_filter( 'document_title_parts', function ( $title ) {

    if ( is_home() || is_front_page() )
        unset($title['tagline']);

    return $title;

}, 10, 1 );

Looking back on this; the pre_get_document_title filter is pretty interesting. Essentially before it processes the title it'll run this filter. If the result isn't empty (which was not expected) then the process is short-circuited.

$title = apply_filters( 'pre_get_document_title', '' );
if ( ! empty( $title ) ) {
    return $title;
}

That means that if you defined the title, it doesn't have to worry about anything else. The nice thing is that you can make exceptions to the rule. So to answer your original question:

add_filter( 'pre_get_document_title', function( $title ) {

    if ( is_home() || is_front_page() ) {

        // Return blog title on front page

        $title = get_bloginfo( 'name' );
    }

    return $title;

} );
0
2

Found a solution:

add_filter( 'pre_get_document_title', function ( $title ) {
    if(is_front_page()){
        $title = get_bloginfo();
    }
    return $title;
});
0
1

The problem is the line:

$title = "$title";

You actually just change the $title to itself. If you change it to

$title = get_bloginfo( 'name' );

the returned title on the front page is going to be the name of your blog. You can put any string there. Also, it is not necessary to call the globals here.

Here is some code that should work:

add_filter( 'wp_title', function ( $title, $sep ) {

    $title .= get_bloginfo( 'name' );

    if ( is_home() || is_front_page() )
        $title = "Any string you want to have";

    return $title;

}, 10, 2 );
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.