wp_register_script with protocol agnostic source parameter

In this WordPress function to register a script wp_register_script can be used, to load external scripts, the follow format:

wp_register_script('my_jQuery', '//ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js');

As appears in documentation the second parameter can begins with “//” to leave WordPress specify the protocol:

Read More

Remote assets can be specified with a protocol-agnostic URL, i.e.
‘//otherdomain.com/js/theirscript.js’. Default: None

For WordPress 3.5.1 works fine, but for WordPress 3.4.2 doesn’t.

Two questions:

1- From which WordPress version can be used the second parameter with “//”?

2- For WordPress versions that doesn’t support this format “//”, which is the recommended way of call the external resource? Should I ask for WordPress page protocol and use accordingly it?

Related posts

Leave a Reply

2 comments

  1. This was discussed in core Ticket #16560:

    When loading external resources, most browsers (need to be confirmed) will auto select between https:// and http:// if the url-scheme is simply //. For example, //ajax.googleapis.com/ajax/libs/jquery/1.5.0/jquery.min.js would load over https if the containing page was https itself, but over http if wasn’t.

    class.wp-scripts.php and class.wp-styles.php does a regex check for ^https?:// to determine if it is an external resource or not, but should really use ^(https?:)?// – or even ^(w+:)?// (since some loony could use ftp for example).

    The ticket was closed and set to fixed eight months ago, but only implemented in version 3.5.

    For cases where you need to manually check the protocol, the function is_ssl() is used:

    $protocol = is_ssl() ? 'https://' : 'http://';
    
  2. Although I do not know which versions of WordPress support the protocol-agnostic URL, I can answer your second question. The purpose of protocol-agnostic URLs is to automatically use either an SSL connection or a regular connection when loading resources based on what is currently being used to load the page. Whether or not you are using an SSL connection on your website, if you want to support older versions of WordPress, then I would use https:// instead of // when possible. Please note, this will only work for external scripts that support an SSL connection, like Google’s hosted scripts. If the resource you are loading does not support SSL, then the choice is clear, just use http:// instead of //