So, I've registered and enqueued my script.


But how do I add a GET variable on the script src url?


Any help appreciated.

1 Answer 1


Filter script_loader_src and use add_query_arg(). You can use parse_url() or the second argument $handle to target specific scripts...

I have included multiple redundant options here:

add_filter( 'script_loader_src', 'wpse_99842_add_get_var_to_url', 10, 2 );

function wpse_99842_add_get_var_to_url( $url, $handle )
    if ( 'my_handle' !== $handle )
        return $url;

    if ( empty ( $_GET['myvar'] ) )
        return $url;

    $parts = parse_url( $url );

    // specific host
    if ( 'example.com' !== $parts['host'] )
        return $url;

    // specific path
    if ( '/foo/bar' !== $parts['path'] )
        return $url;

    // or specific start of path
    if ( 0 !== strpos( $parts['path'], '/foo/' ) )
        return $url;

    return add_query_arg( 'myVar', $_GET['myvar'], $url );

Your Answer

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

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