WordPress.org

Make WordPress Core

Opened 3 years ago

Last modified 12 months ago

#32607 new feature request

Function for URL to registered scripts and styles

Reported by: msnewton Owned by:
Milestone: Awaiting Review Priority: normal
Severity: normal Version:
Component: Script Loader Keywords: has-patch reporter-feedback
Focuses: Cc:

Description

I would like to see a function that would return the full URL to a script or style registered with wp_register_script() or wp_register_style(), or those that are “built-in” to WP.

The reason is this: I generate all of the HTML for special pages myself but wish to include standard scripts and styles that are built in to WordPress. Rather than relying upon a fixed filepath to remain static, and assuming it won't change in future versions, I'd like to ask WP to tell me where it is.

Attachments (1)

32607.patch (3.2 KB) - added by tyxla 3 years ago.
Introducing wp_get_registered_script() and wp_get_registered_style(). Including unit tests.

Download all attachments as: .zip

Change History (5)

#1 @tyxla
3 years ago

Hi @msnewton and welcome to the WordPress Core Trac!

This sounds like a good idea IMHO. I've personally had to locate the URL or version of a registered script several times and I always had to use the global variables $wp_scripts for these instances.

So, it might be useful to be able to quickly find the URL, version or dependancies of a style/script that has been registered.

The functions for that can simply return the _WP_Dependency object (containing handle - script/style name, src - URL, deps - dependancies, ver - version, etc.), or false if the script/style hasn't been registered.

I'll attach a patch for these functions in a minute.

@tyxla
3 years ago

Introducing wp_get_registered_script() and wp_get_registered_style(). Including unit tests.

#2 @tyxla
3 years ago

  • Keywords has-patch added

Here is an example usage with the functions from my patch:

// registering a script
wp_register_script( 'handle', 'http://example.com', array( 'test-dep' ), 1 );

// retrieving various details for the 'handle' script
$script = wp_get_registered_script( 'handle' );
echo $script->src; // script URL
echo $script->ver; // script version

So this way you could easily retrieve the entire script/style dependancy object, and quickly get the URL, version, dependancies or anything else from it.

#3 @SergeyBiryukov
12 months ago

  • Component changed from General to Script Loader

#4 in reply to: ↑ description @SergeyBiryukov
12 months ago

  • Keywords reporter-feedback added
  • Summary changed from Function for URL to registers scripts and styles to Function for URL to registered scripts and styles

Replying to msnewton:

The reason is this: I generate all of the HTML for special pages myself but wish to include standard scripts and styles that are built in to WordPress.

Adding <?php wp_head(); ?> before </head> and <?php wp_footer(); ?> before </body> should do that, unless I'm missing something. Why do you need a new function?

Note: See TracTickets for help on using tickets.