document_title_parts -­­ WordPress Filter Hook

document_title_parts filter hook is used to change the content of <title> tag. It accepts the first argument as an associative array.

There are 4 parts in a WordPress page title.

  • title Common for all pages
  • page Only available for pagination page
  • tagline Only available for homepage
  • site Available for all pages except the homepage

Basic Use:

add_filter( 'document_title_parts', function($title) {
    $title['title'] = 'Your updated title here';

    return $title;
} );

Source (wp-includes/general-template.php)

$title = apply_filters( 'document_title_parts', $title );

Change title by post ID

Let's change the page title for a specific post by ID in WordPress using document_title_part filter hook.</p>

add_filter( 'document_title_parts', function($title) {
    if ( get_the_ID() == 32 ) {
        $title['title'] = 'Your updated title here';
    }
} );

Leave a Reply

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