#44183 closed defect (bug) (fixed)
BUG in get_the_archive_title() when get author
Reported by: |
|
Owned by: |
|
---|---|---|---|
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)
Change History (11)
#2
@
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
@
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.
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()
withget_the_author()
since it returns a pure string value ($authordata->display_name
) picked directly from thewp_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.