Is there any way to add a crossorigin
attribute to a custom registered JavaScript?
wp_register_script('foo', 'http://cdn.domain.com/script.min.js', null, '1.2.3');
wp_enqueue_script( 'bar', '/path/to/bar.js', array( 'foo' ), '20170912' );
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It only takes a minute to sign up.
Sign up to join this communityscript_loader_tag
or script_loader_src
filters are there to let you tweak the HTML of the script easily so you can add custom attributes:
add_filter('script_loader_tag', function($tag, $handle){
switch ( $handle ) {
case 'foo':
$tag = preg_replace(
'/src=[\'|"|]/i',
'crossorigin $0',
$tag
);
break;
}
return $tag;
}, 10, 2);
To avoid conflicting with other plugins, pass unique handles to the script/style register/enqueue function, foo
in your case:
wp_register_script('foo', 'http://cdn.domain.com/script.min.js', null, '1.2.3');
Using wp_register_script/wp_enqueue_script functions, you can't do that. Maybe open the ticket with WordPress TRAC to be implemented in future versions.