1

Is it possible to get a script url from one of my working scripts, by handle?

So for example if i have a plugin's JS file registered with the handle wp-js-plugin and i want to get this JS file's url. how can i do that?

2 Answers 2

3

For more info dump $wp_scripts

function get_script_src_by_handle($handle) {
    global $wp_scripts;
    if(in_array($handle, $wp_scripts->queue)) {
        return $wp_scripts->registered[$handle]->src;
    }
}
Sign up to request clarification or add additional context in comments.

4 Comments

great! Thanks! is there a way to return the script if it is registered or not...? (will '$wp_scripts->[$handle]->src' work?)
I'm not sure what you're trying to say.
Let's say this script is deregistered and i want to get is url. is it possible?
I do not think it's possible.
0

Simple:

const scriptUrls = [...document.querySelectorAll('script')].map(e=>e.src)

This will list all the urls for each script.

1 Comment

Maybe i wasn't clear in my question. i'm looking for a way of echoing a specific script url. ("i will give you the scripts handle and you will echo the script's url)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.