WordPress.org

Make WordPress Core

Opened 4 years ago

Closed 4 years ago

Last modified 4 years ago

#34962 closed defect (bug) (fixed)

Issues with wp_get_document_title function causing problems with document titles

Reported by: kasia_codeword Owned by: obenland
Milestone: 4.4.1 Priority: normal
Severity: normal Version: 4.4
Component: Themes Keywords: has-patch has-unit-tests
Focuses: Cc:
PR Number:

Description

Please see this thread on the support forum as a primer:

https://wordpress.org/support/topic/document-titles-in-44

Document titles for site front pages are not being generated correctly if Front page and Posts page are set to static pages in Reading Settings. From what I can tell, the issue is related to three sections of code in wp-includes/general-template.php

First, lines 859-861

	// If on the home or front page, use the site title.
	} elseif ( is_home() && is_front_page() ) {
		$title['title'] = get_bloginfo( 'name', 'display' );

This is actually saying that if is_home and is_front_page both return true, then use the site title, however the only way that both is_home and is_front_page can return true is if Front page is set to "your latest posts" in Reading Settings. Therefore, if a static page is selected as the front page in Reading Settings, the site title will not be used.

Then, lines 871-876

/*
	 * If we're on the blog page and that page is not the homepage or a single
	 * page that is designated as the homepage, use the container page's title.
	 */
	} elseif ( ( is_home() && ! is_front_page() ) || ( ! is_home() && is_front_page() ) ) {
		$title['title'] = single_post_title( '', false );

This is saying that if it is the blog index but the blog index is not the home page of the site, OR if it is the front page of the site but the front page is not the blog index (in other words, a static page is the front page) then use the single post title.

So up to this point if I have a static page called Home set as my front page, what returns as the document title is Home - Site Title rather than Site Title - Tagline.

Finally, lines 906-911

// Append the description or site title to give context.
	if ( is_home() && is_front_page() ) {
		$title['tagline'] = get_bloginfo( 'description', 'display' );
	} else {
		$title['site'] = get_bloginfo( 'name', 'display' );
	}

This is saying to append the tag line only if is_home and is_front_page both return true, otherwise append the site title. The only way both can return true is if "Front page displays" in Reading Settings is always set to "your latest posts".

So basically all of this means that anyone with a site configuration where the front page and posts page are set to static pages cannot use the Site Title - Tagline configuration for the document title on the front page of the site.

I fiddled around a bit with the code, I'm not sure that this is entirely correct in terms of best practice but I was able to get things to behave as they used to in this regard in 4.3.1 by making the following changes:

Lines 859-861

// If on the home or front page, use the site title.
	} elseif ( is_front_page() ) {
		$title['title'] = get_bloginfo( 'name', 'display' );

using the logic that regardless of whether Front Page is set to "your latest posts" or a static page in Reading settings, is_front_page will return as true in both cases.

Lines 875-876

} elseif ( is_home() && ! is_front_page() ) {
		$title['title'] = single_post_title( '', false );

using the logic that we would only want to use the single post title if it is a blog post archive that is not the front page where is_home returns true but is_front_page returns false.

Lines 906-911

// Append the description or site title to give context.
	if ( is_front_page() ) {
		$title['tagline'] = get_bloginfo( 'description', 'display' );
	} else {
		$title['site'] = get_bloginfo( 'name', 'display' );
	}

again using the logic that is_front_page returns true regardless of whether "latest posts" or static page is selected as the front page.

Attachments (3)

34962.diff (2.2 KB) - added by peterwilsoncc 4 years ago.
34962.2.diff (3.7 KB) - added by obenland 4 years ago.
34962.3.diff (4.0 KB) - added by peterwilsoncc 4 years ago.

Download all attachments as: .zip

Change History (16)

#1 @swissspidy
4 years ago

  • Summary changed from Issues with wp_get_document_title function in wp-includes/general-template.php causing problems with document titles to Issues with wp_get_document_title function causing problems with document titles

#2 @SergeyBiryukov
4 years ago

  • Component changed from General to Themes
  • Milestone changed from Awaiting Review to 4.4.1

Hi @kasia_codeword, thanks for the report!

#3 @peterwilsoncc
4 years ago

  • Keywords needs-patch added

@peterwilsoncc
4 years ago

#4 @peterwilsoncc
4 years ago

  • Keywords has-patch has-unit-tests added; needs-patch removed

The unit tests and the comments in wp_get_document_title do not match.

In 34962.diff I've assumed the comments are correct, can you correct me if I'm wrong about #31078 @obenland?

  • Added unit test for the blog page
  • front-page title: Site name - description
  • blog-page: Page name - site name

#5 @jorbin
4 years ago

  • Owner set to obenland
  • Status changed from new to assigned

@obenland can you take a look?

@obenland
4 years ago

#6 @obenland
4 years ago

Iterated on @peterwilsoncc's excellent patch. What do y'all think of the unit tests?

This ticket was mentioned in Slack in #core by jorbin. View the logs.


4 years ago

#8 @peterwilsoncc
4 years ago

On manual testing, this looks good. 34962.3.diff updates a comment in the second patch, highlighted below.

/src/ looks ready to commit.

-       // If on the home or front page, use the site title.
-       } elseif ( is_home() && is_front_page() ) {
+       // If on the front page, use the site title.
+       } elseif ( is_front_page() ) {

Unit tests look good, but will defer to others as they're not my strong point.

This ticket was mentioned in Slack in #core by jorbin. View the logs.


4 years ago

#10 @nacin
4 years ago

I mostly follow this logic. The patch looks good.

For the unit test: there's no need to reset the option at the end of the test. This would only run if the test succeeded — if the test failed, the reset would happen and then it'd spill over into other tests. The fix normally would be to thus use a tearDown() method. However, in this case, our WP_UnitTestCase automatically rolls back database writes as well as resets the internal object cache. So given that, we can just remove that line.

This ticket was mentioned in Slack in #core by jorbin. View the logs.


4 years ago

#12 @obenland
4 years ago

  • Resolution set to fixed
  • Status changed from assigned to closed

In 36168:

Template: Always display the site title on the front page.

Limits using the page title to the blog page when the site has a static front page,
bringing it N’Sync with wp_title().

Props peterwilsoncc.
Fixes #34962.

#13 @obenland
4 years ago

In 36169:

Template: Always display the site title on the front page.

Limits using the page title to the blog page when the site has a static front page,
bringing it N’Sync with wp_title().

Merges [36168] to the 4.4 branch.

Props peterwilsoncc.
Fixes #34962.

Note: See TracTickets for help on using tickets.