Generally, I escape URLs when outputting content with esc_url
like so esc_url( get_home_url() )
. If I'd like to adding a trailing slash e.g. /
, I'm combining it with esc_url
by prepending. An example, trailingslashit( esc_url( get_home_url() ) )
. I'm assuming it's the correct way of using it rather than other permutations e.g. esc_url( trailingslashit( get_home_url() ) )
1 Answer
It is recommended to escape as late as possible, but the function simply adds a trailing slash (/
) (after removing existing one, if any), and I noticed core also used trailingslashit( esc_url() )
, so I guess that's how we would do it.
- But that is not a definitive answer to your question (which is interesting, BTW), because there's no official statement regarding which format should be used.
However, there's a similar function, namely user_trailingslashit()
, which conditionally adds the trailing slash and applies a filter hook that's also named user_trailingslashit
, hence in that case, the format should always be esc_url( user_trailingslashit() )
.
-
1