3 * BackPress Scripts Procedural API
8 * @subpackage BackPress
12 * Print scripts in document head that are in the $handles queue.
14 * Called by admin-header.php and wp_head hook. Since it is called by wp_head on every page load,
15 * the function does not instantiate the WP_Scripts object unless script names are explicitly passed.
16 * Makes use of already-instantiated $wp_scripts global if present. Use provided wp_print_scripts
17 * hook to register/enqueue new scripts.
19 * @see WP_Scripts::do_items()
20 * @global WP_Scripts $wp_scripts The WP_Scripts object for printing scripts.
24 * @param array|bool $handles Optional. Scripts to be printed. Default 'false'.
25 * @return array On success, a processed array of WP_Dependencies items; otherwise, an empty array.
27 function wp_print_scripts( $handles = false ) {
28 do_action( 'wp_print_scripts' );
29 if ( '' === $handles ) // for wp_head
33 if ( ! is_a( $wp_scripts, 'WP_Scripts' ) ) {
34 if ( ! did_action( 'init' ) )
35 _doing_it_wrong( __FUNCTION__, sprintf( __( 'Scripts and styles should not be registered or enqueued until the %1$s, %2$s, or %3$s hooks.' ),
36 '<code>wp_enqueue_scripts</code>', '<code>admin_enqueue_scripts</code>', '<code>login_enqueue_scripts</code>' ), '3.3' );
39 return array(); // No need to instantiate if nothing is there.
41 $wp_scripts = new WP_Scripts();
44 return $wp_scripts->do_items( $handles );
48 * Register a new script.
50 * Registers a script to be linked later using the wp_enqueue_script() function.
52 * @see WP_Dependencies::add(), WP_Dependencies::add_data()
53 * @global WP_Scripts $wp_scripts The WP_Scripts object for printing scripts.
57 * @param string $handle Name of the script. Should be unique.
58 * @param string $src Path to the script from the WordPress root directory. Example: '/js/myscript.js'.
59 * @param array $deps Optional. An array of registered script handles this script depends on. Set to false if there
60 * are no dependencies. Default empty array.
61 * @param string|bool $ver Optional. String specifying script version number, if it has one, which is concatenated
62 * to end of path as a query string. If no version is specified or set to false, a version
63 * number is automatically added equal to current installed WordPress version.
64 * If set to null, no version is added. Default 'false'. Accepts 'false', 'null', or 'string'.
65 * @param bool $in_footer Optional. Whether to enqueue the script before </head> or before </body>.
66 * Default 'false'. Accepts 'false' or 'true'.
68 function wp_register_script( $handle, $src, $deps = array(), $ver = false, $in_footer = false ) {
70 if ( ! is_a( $wp_scripts, 'WP_Scripts' ) ) {
71 if ( ! did_action( 'init' ) )
72 _doing_it_wrong( __FUNCTION__, sprintf( __( 'Scripts and styles should not be registered or enqueued until the %1$s, %2$s, or %3$s hooks.' ),
73 '<code>wp_enqueue_scripts</code>', '<code>admin_enqueue_scripts</code>', '<code>login_enqueue_scripts</code>' ), '3.3' );
74 $wp_scripts = new WP_Scripts();
77 $wp_scripts->add( $handle, $src, $deps, $ver );
79 $wp_scripts->add_data( $handle, 'group', 1 );
85 * Works only if the script has already been added.
87 * Accepts an associative array $l10n and creates a JavaScript object:
96 * @see WP_Dependencies::localize()
97 * @link http://core.trac.wordpress.org/ticket/11520
98 * @global WP_Scripts $wp_scripts The WP_Scripts object for printing scripts.
102 * @param string $handle Script handle the data will be attached to.
103 * @param string $object_name Name for the JavaScript object. Passed directly, so it should be qualified JS variable.
104 * Example: '/[a-zA-Z0-9_]+/'.
105 * @param array $l10n The data itself. The data can be either a single or multi-dimensional array.
106 * @return bool True if the script was successfully localized, false otherwise.
108 function wp_localize_script( $handle, $object_name, $l10n ) {
110 if ( ! is_a( $wp_scripts, 'WP_Scripts' ) ) {
111 if ( ! did_action( 'init' ) )
112 _doing_it_wrong( __FUNCTION__, sprintf( __( 'Scripts and styles should not be registered or enqueued until the %1$s, %2$s, or %3$s hooks.' ),
113 '<code>wp_enqueue_scripts</code>', '<code>admin_enqueue_scripts</code>', '<code>login_enqueue_scripts</code>' ), '3.3' );
118 return $wp_scripts->localize( $handle, $object_name, $l10n );
122 * Remove a registered script.
124 * Note: there are intentional safeguards in place to prevent critical admin scripts,
125 * such as jQuery core, from being unregistered.
127 * @see WP_Dependencies::remove()
128 * @global WP_Scripts $wp_scripts The WP_Scripts object for printing scripts.
132 * @param string $handle Name of the script to be removed.
134 function wp_deregister_script( $handle ) {
136 if ( ! is_a( $wp_scripts, 'WP_Scripts' ) ) {
137 if ( ! did_action( 'init' ) )
138 _doing_it_wrong( __FUNCTION__, sprintf( __( 'Scripts and styles should not be registered or enqueued until the %1$s, %2$s, or %3$s hooks.' ),
139 '<code>wp_enqueue_scripts</code>', '<code>admin_enqueue_scripts</code>', '<code>login_enqueue_scripts</code>' ), '3.3' );
140 $wp_scripts = new WP_Scripts();
144 * Do not allow accidental or negligent de-registering of critical scripts in the admin.
145 * Show minimal remorse if the correct hook is used.
147 $current_filter = current_filter();
148 if ( ( is_admin() && 'admin_enqueue_scripts' !== $current_filter ) ||
149 ( 'wp-login.php' === $GLOBALS['pagenow'] && 'login_enqueue_scripts' !== $current_filter )
152 'jquery', 'jquery-core', 'jquery-migrate', 'jquery-ui-core', 'jquery-ui-accordion',
153 'jquery-ui-autocomplete', 'jquery-ui-button', 'jquery-ui-datepicker', 'jquery-ui-dialog',
154 'jquery-ui-draggable', 'jquery-ui-droppable', 'jquery-ui-menu', 'jquery-ui-mouse',
155 'jquery-ui-position', 'jquery-ui-progressbar', 'jquery-ui-resizable', 'jquery-ui-selectable',
156 'jquery-ui-slider', 'jquery-ui-sortable', 'jquery-ui-spinner', 'jquery-ui-tabs',
157 'jquery-ui-tooltip', 'jquery-ui-widget', 'underscore', 'backbone',
160 if ( in_array( $handle, $no ) ) {
161 $message = sprintf( __( 'Do not deregister the %1$s script in the administration area. To target the frontend theme, use the %2$s hook.' ),
162 "<code>$handle</code>", '<code>wp_enqueue_scripts</code>' );
163 _doing_it_wrong( __FUNCTION__, $message, '3.6' );
168 $wp_scripts->remove( $handle );
174 * Registers the script if $src provided (does NOT overwrite), and enqueues it.
176 * @see WP_Dependencies::add(), WP_Dependencies::add_data(), WP_Dependencies::enqueue()
177 * @global WP_Scripts $wp_scripts The WP_Scripts object for printing scripts.
181 * @param string $handle Name of the script.
182 * @param string|bool $src Path to the script from the root directory of WordPress. Example: '/js/myscript.js'.
183 * @param array $deps An array of registered handles this script depends on. Default empty array.
184 * @param string|bool $ver Optional. String specifying the script version number, if it has one. This parameter
185 * is used to ensure that the correct version is sent to the client regardless of caching,
186 * and so should be included if a version number is available and makes sense for the script.
187 * @param bool $in_footer Optional. Whether to enqueue the script before </head> or before </body>.
188 * Default 'false'. Accepts 'false' or 'true'.
190 function wp_enqueue_script( $handle, $src = false, $deps = array(), $ver = false, $in_footer = false ) {
192 if ( ! is_a( $wp_scripts, 'WP_Scripts' ) ) {
193 if ( ! did_action( 'init' ) )
194 _doing_it_wrong( __FUNCTION__, sprintf( __( 'Scripts and styles should not be registered or enqueued until the %1$s, %2$s, or %3$s hooks.' ),
195 '<code>wp_enqueue_scripts</code>', '<code>admin_enqueue_scripts</code>', '<code>login_enqueue_scripts</code>' ), '3.3' );
196 $wp_scripts = new WP_Scripts();
200 $_handle = explode('?', $handle);
201 $wp_scripts->add( $_handle[0], $src, $deps, $ver );
203 $wp_scripts->add_data( $_handle[0], 'group', 1 );
205 $wp_scripts->enqueue( $handle );
209 * Remove a previously enqueued script.
211 * @see WP_Dependencies::dequeue()
212 * @global WP_Scripts $wp_scripts The WP_Scripts object for printing scripts.
216 * @param string $handle Name of the script to be removed.
218 function wp_dequeue_script( $handle ) {
220 if ( ! is_a( $wp_scripts, 'WP_Scripts' ) ) {
221 if ( ! did_action( 'init' ) )
222 _doing_it_wrong( __FUNCTION__, sprintf( __( 'Scripts and styles should not be registered or enqueued until the %1$s, %2$s, or %3$s hooks.' ),
223 '<code>wp_enqueue_scripts</code>', '<code>admin_enqueue_scripts</code>', '<code>login_enqueue_scripts</code>' ), '3.3' );
224 $wp_scripts = new WP_Scripts();
227 $wp_scripts->dequeue( $handle );
231 * Check whether a script has been added to the queue.
233 * @global WP_Scripts $wp_scripts The WP_Scripts object for printing scripts.
236 * @since 3.5.0 'enqueued' added as an alias of the 'queue' list.
238 * @param string $handle Name of the script.
239 * @param string $list Optional. Status of the script to check. Default 'enqueued'.
240 * Accepts 'enqueued', 'registered', 'queue', 'to_do', and 'done'.
241 * @return bool Whether the script script is queued.
243 function wp_script_is( $handle, $list = 'enqueued' ) {
245 if ( ! is_a( $wp_scripts, 'WP_Scripts' ) ) {
246 if ( ! did_action( 'init' ) )
247 _doing_it_wrong( __FUNCTION__, sprintf( __( 'Scripts and styles should not be registered or enqueued until the %1$s, %2$s, or %3$s hooks.' ),
248 '<code>wp_enqueue_scripts</code>', '<code>admin_enqueue_scripts</code>', '<code>login_enqueue_scripts</code>' ), '3.3' );
249 $wp_scripts = new WP_Scripts();
252 return (bool) $wp_scripts->query( $handle, $list );