1

I've added following URL in my theme, but I have been informed that I must use esc_url(). I could not figure out, how can I use that function with following code?

<a href="http://pinterest.com/pin/create/button/?url=<?php echo urlencode( get_the_permalink() ); ?>&amp;media=<?php echo urlencode($thumbnail); ?>&amp;description=<?php echo urlencode( get_the_title() ); ?>" target="_blank">Pinterest</a> 
5
  • doesn't look like you need esc_url or even the urlencode you use except for the title portion Jan 19, 2016 at 6:33
  • I thought so too, but a theme reviewer insists :$
    – kashan
    Jan 19, 2016 at 6:43
  • I you think you need it. You are using the title in a URL and you are getting the title dinamically; so you never know exactly if the title is URL-econded or not. Better to use it. Just one example: it is very common that titles contain white spaces and white spaces need to be enconded to be used in URLs.
    – cybmeta
    Jan 19, 2016 at 6:58
  • @cybmeta can you give an example how can i use that function with above url?
    – kashan
    Jan 19, 2016 at 7:04
  • Sure, see answer.
    – cybmeta
    Jan 19, 2016 at 7:07

1 Answer 1

1

I you think you need it. For example, you are using the title in a URL and you are getting the title dinamically; so you never know exactly if the title is valid to be used in URL or not. So, it is better to use it.

Just one example: it is very common that titles contain white spaces and white spaces need to be enconded to be used in URLs.

I would build the full URL string first and then I would use esc_url() at moment it is used for some attribute value.

$url_params = array(
    'url'         => get_the_permalink(),
    'media'       => $thumbnail,
    'description' => get_the_title()
);

$url = "http://pinterest.com/pin/create/button/?" . http_build_query( $url_params );

echo '<a href="' . esc_url( $url ) . '">Pinterest</a>';

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.