Skip to content

Instantly share code, notes, and snippets.

@kanlukasz
Forked from kovshenin/something.php
Created September 17, 2021 07:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kanlukasz/5b3f77f630108eeed68e1926db59c509 to your computer and use it in GitHub Desktop.
Save kanlukasz/5b3f77f630108eeed68e1926db59c509 to your computer and use it in GitHub Desktop.
Yes, you can use printf and sprintf in WordPress too!
<?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() ) );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment