I have seen a few WordPress sites and tutorials enqueuing fonts like this. Does this work with http and https sites? Is it safe to do this? I see these exact URL's in the source code when testing this function on an http site.

add_action('wp_enqueue_scripts', 'theme_external_styles');
function theme_external_styles(){
    wp_enqueue_style( 'main_css', '//maxcdn.bootstrapcdn.com/font-awesome/4.2.0/css/font-awesome.min.css' );
    wp_enqueue_style( 'roboto', '//fonts.googleapis.com/css?family=Roboto:400,300,100,500,700,500italic,400italic,300italic,100italic,700italic' );
  • Yes. That is protocol relative URL and should work assuming the resource will load over either protocol. – s_ha_dum Jul 8 '15 at 18:58
  • Thank you. Would this work on a production site that is only https? @s_ha_dum – JediTricks007 Jul 8 '15 at 19:00
  • The problem will be with whether your resources load over http and https as the request will be sent with whatever the site is using. – s_ha_dum Jul 8 '15 at 19:11

Yes this is fine and is a good practice, especially if your site may switch between HTTPS/HTTP (like for a shopping cart etc) that way you are not loading mixed content.

As long as the CDN you are pulling from offers both HTTPS and HTTP you're fine, which they do.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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