Make WordPress Core

Opened 5 years ago

Closed 2 years ago

Last modified 2 years ago

#44183 closed defect (bug) (fixed)

BUG in get_the_archive_title() when get author

Reported by: tkama's profile Tkama Owned by: johnbillion's profile johnbillion
Milestone: 5.7 Priority: normal
Severity: normal Version: 4.9.6
Component: Query Keywords: has-patch has-unit-tests
Focuses: Cc:

Description

On author archive page with no posts get_the_archive_title() don't display author name - it return empty string. Example: https://wp-kama.com/author/vladlu

It is because get_the_author() which need global var $authordata, but this var is empty if user dont have any posts...

	} elseif ( is_author() ) {
		/* translators: Author archive title. 1: Author name */
		$title = sprintf( __( 'Author: %s' ), '<span class="vcard">' . get_the_author() . '</span>' );
	} elseif ( is_year() ) {

We need to think about the way to fix this. Maybe good solution is to set global $authordata for request with no posts in WP::register_globals(). Something like this:

	if ( $wp_query->is_author() ){
		if( isset( $wp_query->post ) )
			$GLOBALS['authordata'] = get_userdata( $wp_query->post->post_author );
		else
			$GLOBALS['authordata'] = get_userdata( get_queried_object()->ID );
	}

-

One more notice: it's better to use esc_html() for get_the_author() in get_the_archive_title().

	} elseif ( is_author() ) {
		/* translators: Author archive title. 1: Author name */
		$title = sprintf( __( 'Author: %s' ), '<span class="vcard">' . esc_html( get_the_author() ) . '</span>' );
	} elseif ( is_year() ) {

Attachments (3)

44183.diff (551 bytes) - added by subrataemfluence 5 years ago.
44183.2.diff (511 bytes) - added by johnbillion 2 years ago.
44183.3.diff (1.8 KB) - added by johnbillion 2 years ago.

Download all attachments as: .zip

Change History (11)

#1 @subrataemfluence
5 years ago

  • Component changed from General to Users
  • Keywords has-patch added

Good point! Based on your recommendation I am submitting a proposed patch for $authordata.

However, I don't see it is super necessary to include an esc_html() with get_the_author() since it returns a pure string value ($authordata->display_name) picked directly from the wp_users table.

I don't think we have the ability to use HTML formatting for display_name field. Please correct me if I am wrong.

Last edited 5 years ago by subrataemfluence (previous) (diff)

#2 @Tkama
5 years ago

  • Version set to 4.9.6

I don't think we have the ability to use HTML formatting for display_name field. Please correct me if I am wrong.

It's a good practice to esc any vulnerable string on output. For example, some theme allows to change 'display_name' but don't sanitize the value on save, and in this case, WP will output the string as it is...

I'm not sure that's really necessary. Because get_the_archive_title() only return the string, but not echo it. But on the other hand, we have there '<span class="vcard">' html tag and we can't esc the value in future.

#3 @johnbillion
2 years ago

  • Component changed from Users to Query
  • Keywords needs-refresh added
  • Milestone changed from Awaiting Review to 5.7
  • Owner set to johnbillion
  • Status changed from new to reviewing

Thanks for reporting the issue @Tkama . Sorry it's taken so long.

Thanks for the patch @subrataemfluence . There's no need for the $authordata value to be conditional, it should always be fetched from the queried object instead of the first post in the query result.

@johnbillion
2 years ago

#4 @johnbillion
2 years ago

  • Keywords needs-refresh removed

@johnbillion
2 years ago

#5 @johnbillion
2 years ago

  • Keywords has-unit-tests added

#6 @johnbillion
2 years ago

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

In 49843:

Query: Ensure the author archive title always shows the name of the queried author, regardless of whether there are results.

This brings the behaviour inline with the <title> element of the page which always shows the author name.

Props Tkama, subrataemfluence

Fixes #44183

#7 @johnbillion
2 years ago

In 49846:

Query: Correct some coding standards after [49843].

See #44183

#8 @peterwilsoncc
2 years ago

In 49847:

Query: Correct some coding standards after [49843].

See #44183

Note: See TracTickets for help on using tickets.