Make WordPress Core

Opened 9 years ago

Closed 9 years ago

Last modified 9 years ago

#29918 closed enhancement (invalid)

Allow script localization to be set on wp_register_script

Reported by: jadpm's profile jadpm Owned by:
Milestone: Priority: normal
Severity: normal Version: 4.0
Component: Script Loader Keywords:
Focuses: Cc:

Description

Not sure if that has been asked before, could not find it.

Let script_a.js be a script that needs some localization. Let script_b.js be a script that has script_a.js as a dependency. We can register both of them with the right dependency settings, and enqueue just script_b.js. This will enqueue script_a.js, but we have no way of localizing it. The only way to do so is to enqueue script_a.js manually, making the dependency feature useless.

The goal should be to have the localization data ready on enqueue time, no matter whether the script is being used as a dependency or not. As you can add multiple localizations to the same script, this should not break existing calls to wp_localize_script.

I see that the registering of both styles and scripts is handled by the same base method WP_Dependencies::add, so this might not be as simple as to add another parameter to wp_register_script. Anyhow, this would be a very nice feature.

Change History (3)

#1 @ocean90
9 years ago

  • Type changed from feature request to enhancement

Shouldn't this work:

wp_register_script( 'script_a', ... )
wp_localize_script( 'script_a', ... )
wp_register_script( 'script_b', ..., array( 'script_a' )  );
wp_enqueue_script( 'script_b' );

#2 @jadpm
9 years ago

  • Resolution set to invalid
  • Status changed from new to closed

I was under the wrong impression that you needed to enqueue the script to be able to localize it, but I see it is enough to have it registered. My bad. Closing as invalid.

#3 @ocean90
9 years ago

  • Milestone Awaiting Review deleted
Note: See TracTickets for help on using tickets.