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' );

2 Answers 2


script_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(
                'crossorigin $0',

    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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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