Created
July 10, 2012 07:17
-
-
Save kovshenin/3081766 to your computer and use it in GitHub Desktop.
Yes, you can use printf and sprintf in WordPress too!
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// Dirty, easy to miss a ' or " or . | |
echo '<a href="' . get_permalink() . '" class="link">' . get_the_title() . '</a>'; | |
// Clean, easier to read | |
printf( '<a href="%s" class="link">%s</a>', get_permalink(), get_the_title() ); | |
// Almost as clean, and more secure, maybe a little paranoic :) | |
printf( '<a href="%s" class="link">%s</a>', esc_url( get_permalink() ), esc_html( get_the_title() ) ); |
@corvannoorloos it's up to you, but the second snippet looks cleaner to me, but I'd go slightly further:
function cor_canonical_link() {
printf( "<link href='%s' rel='canonical' />\n", get_pagenum_link( get_query_var( 'paged' ) ) );
}
@kovshenin thank you! I wasn't aware of this use of get_query_var
.
Personally I prefer to use double quotes on the href
and rel
, but this definitely gives me something to work with.
Good tip boss, thanks
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Being somewhat novice at writing php, are there any exceptions when it's better to write something else?
In example, would it make any sense rewriting:
to