14

According to the Codex, wp_enqueue_script supports protocol relative, or protocol agnostic external links: "Remote assets can be specified with a protocol-agnostic URL, i.e. '//otherdomain.com/js/theirscript.js'."

But I'm not seeing it:

wp_enqueue_script('google-maps', '//maps.googleapis.com/maps/api/js?&sensor=false', array(), '3', true);

Output:

<script type='text/javascript' src='http://localhost:25898//maps.googleapis.com/maps/api/js?sensor=false&#038;ver=3'></script>

Notice that the protocol relative URL is appended to the site URL.

18

The code you posted works fine and results in this in the HTML output:

<script type='text/javascript' src='//maps.googleapis.com/maps/api/js?sensor=false&#038;ver=3'></script>

Tested on WordPress 3.5 with this code snippet:

add_action('wp_enqueue_scripts', 'test');
function test() {
  wp_enqueue_script('google-maps', '//maps.googleapis.com/maps/api/js?&sensor=false', array(), '3', true);
}
7
  • Do you know it this is new in WordPress 3.5? – Michiel van Oosterhout Jan 25 '13 at 13:12
  • Protocol-relative URLs was added 7 months ago: core.trac.wordpress.org/ticket/16560 – Otto Jan 25 '13 at 16:25
  • Ok, so it's not the version of WordPress that I'm using. So what could cause the difference in behaviour? – Michiel van Oosterhout Jan 28 '13 at 9:59
  • 1
    Well, that code was added 7 months ago, but if you look at the ticket, it has a 3.5 milestone, so yes it's probably new behavior to 3.5. – Otto Jan 28 '13 at 22:13
  • Oh right, I was looking at the 'Version' field which was set to 3.1, but that's the version of WordPress at the time the ticket was created. – Michiel van Oosterhout Jan 29 '13 at 12:09

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.