Escaping get_field values in template files – best practice?
-
Are both of these acceptable? Are both safe? Is either one preferred?
Escape at every instance of data output:
$some_url = get_field('some_url'); echo '<p>Visit <a href="' . esc_url($some_url) . '">this website</a></p>'; echo '<p>Really!! Visit <a href="' . esc_url($some_url) . '">this website</a></p>';
Escape only once at point of data retrieval:
$some_url = esc_url(get_field('some_url')); echo '<p>Visit <a href="' . $some_url . '">this website</a></p>'; echo '<p>Really!! Visit <a href="' . $some_url . '">this website</a></p>';
If we are going to be using the piece of data in multiple places, the second can avoid a little duplication. Is it safe enough?
If we make a habit of escaping the data when we first assign the variable, are we safe wherever we us it, as long as we are careful to never use
the_field('some_url')
unescaped?
Viewing 1 replies (of 1 total)
Viewing 1 replies (of 1 total)
- The topic ‘Escaping get_field values in template files – best practice?’ is closed to new replies.