Wordpress - Enqueue Google Web Fonts Without Messing Up Symbols In URL
WordPress knows what it is doing here. Honest.
When rendering an ampersand in HTML, you should always use &
or &
. The browser then converts it to &
before actually firing the HTTP request. See for yourself by inspecting the network calls in a web inspector tool. You're not actually losing your non-latin subsets.
Notice the
&
? It should be &, otherwise the font-files served only have the latin glyphs (i.e. the subset parameter in the URL is neglected unless you use & and NOT its HTML entity).
This tells me you have inspected the source to see that there is an escaped ampersand, without actually verifying the resulting behavior. Yes, it occurs when you paste a URL with an escaped ampersand in an address bar. But not when you have a properly encoded and escaped URL in an HTML src or href attribute.
You should look over http://www.blooberry.com/indexdot/html/topics/urlencoding.htm for additional unsafe and reserved characters. Both groups should always be encoded.
Try this (will also handle HTTP vs HTTPS):
function wpse_google_webfonts() {
$protocol = is_ssl() ? 'https' : 'http';
$query_args = array(
'family' => 'Ubuntu+Condensed|Open+Sans:400italic,700italic,400,700',
'subset' => 'latin,latin-ext',
);
wp_enqueue_style('google-webfonts',
add_query_arg($query_args, "$protocol://fonts.googleapis.com/css" ),
array(), null);
}
add_action( 'wp_enqueue_scripts', 'wpse_google_webfonts' );
Based on this answer you could try something like this untested code:
add_filter( 'clean_url', 'wpse_77227_decode_googlefont_url' );
function wpse_77227_decode_googlefont_url( $url )
{
$host = parse_url ($url, PHP_URL_HOST );
if ( 'fonts.googleapis.com' === $host )
return urldecode( $url );
return $url;
}