Newline URL entity when using esc_url()

Background:

WordPress strips out %0A from URL because in general newline has no practical purpose in URL. Unless it’s a mailto: protocol, WordPress strips out %0A.

So it’s better not to use them in URL at all.

Solution:

Still, if for any specific purpose you have to use %0A, you may use %250A and then filter it back to %0A using clean_url filter.

%250A is URL encoded version of %0A, i.e. double URL encoded version of newline.

You may use the following CODE for filtering (in a plugin or in your active theme’s functions.php file):

add_filter( 'clean_url', 'double_encoded_nl_to_encoded_nl' );
function double_encoded_nl_to_encoded_nl( $url ) {
    return str_ireplace( '%250A', '%0A', $url );
}

Now, this: esc_url( "http://example.com/?q=foo%250Abar" ); will return http://example.com/?q=foo%0Abar.