0

So, I've registered and enqueued my script.

http://mysite.com/wp-content/plugins/my-plugin/script.js?ver=3.6

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

http://mysite.com/wp-content/plugins/my-plugin/script.js?ver=3.6&myVar=whatever

Any help appreciated.

2

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, privacy policy and cookie policy

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