#30319 closed defect (bug) (fixed)
Twenty Fifteen: Incorrect use of sprintf() and _x() in template-tags.php
Reported by: | aprea | Owned by: | |
---|---|---|---|
Milestone: | 4.1 | Priority: | normal |
Severity: | normal | Version: | 4.1 |
Component: | Bundled Theme | Keywords: | has-patch |
Focuses: | Cc: |
Description
The current /inc/template-tags.php
has several incorrect uses of sprintf()
and _x
, see example below:
if ( is_attachment() && wp_attachment_is_image() ) { // Retrieve attachment metadata. $metadata = wp_get_attachment_metadata(); printf( '<span class="full-size-link">%1$s<a href="%2$s">%3$s × %4$s</a></span>', sprintf( _x( '<span class="screen-reader-text">Full size</span>', 'Used before full size attachment link.', 'twentyfifteen' ) ), esc_url( wp_get_attachment_url() ), $metadata['width'], $metadata['height'] ); }
Two problems here:
sprintf()
is only passing 1 argument._x()
includes HTML markup in the replacement text.
Attachments (2)
Change History (10)
#5
@
10 years ago
Thanks obenland!
I was speaking to Dion about this last night on Slack, he suggested that it was perhaps done this way to improve code readability.
I've attached another patch that removes the sprintf()
s and places the screen reader text wrapper directly in the first format string as you suggested.
@
10 years ago
Removes the sprintf()
s and places the screen reader text wrapper directly in the first format string.
#6
@
10 years ago
- Resolution set to fixed
- Status changed from new to closed
Fixed in r30312.
Nice work, @aprea!
#7
@
10 years ago
- Milestone changed from Awaiting Review to 4.1
FWIW, @aprea, I was thinking something like this, not using the extra sprintf:
printf( '<span class="full-size-link">%1$s<a href="%2$s">%3$s × %4$s</a></span>', '<span class="screen-reader-text">' . _x( 'Full size', 'Used before full size attachment link.', 'twentyfifteen' ) . '</span>', esc_url( wp_get_attachment_url() ), $metadata['width'], $metadata['height'] );
Of course, it's fine-as-is in r30312 :)
Note: See
TracTickets for help on using
tickets.
If the screen reader text wrapper would be placed in the first format string,
sprintf()
could be removed all together.