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 ) {
29 * Fires before scripts in the $handles queue are printed.
33 do_action( 'wp_print_scripts' );
34 if ( '' === $handles ) // for wp_head
38 if ( ! is_a( $wp_scripts, 'WP_Scripts' ) ) {
39 if ( ! did_action( 'init' ) )
40 _doing_it_wrong( __FUNCTION__, sprintf( __( 'Scripts and styles should not be registered or enqueued until the %1$s, %2$s, or %3$s hooks.' ),
41 '<code>wp_enqueue_scripts</code>', '<code>admin_enqueue_scripts</code>', '<code>login_enqueue_scripts</code>' ), '3.3' );
44 return array(); // No need to instantiate if nothing is there.
46 $wp_scripts = new WP_Scripts();
49 return $wp_scripts->do_items( $handles );
53 * Register a new script.
55 * Registers a script to be linked later using the wp_enqueue_script() function.
57 * @see WP_Dependencies::add(), WP_Dependencies::add_data()
58 * @global WP_Scripts $wp_scripts The WP_Scripts object for printing scripts.
62 * @param string $handle Name of the script. Should be unique.
63 * @param string $src Path to the script from the WordPress root directory. Example: '/js/myscript.js'.
64 * @param array $deps Optional. An array of registered script handles this script depends on. Set to false if there
65 * are no dependencies. Default empty array.
66 * @param string|bool $ver Optional. String specifying script version number, if it has one, which is concatenated
67 * to end of path as a query string. If no version is specified or set to false, a version
68 * number is automatically added equal to current installed WordPress version.
69 * If set to null, no version is added. Default 'false'. Accepts 'false', 'null', or 'string'.
70 * @param bool $in_footer Optional. Whether to enqueue the script before </head> or before </body>.
71 * Default 'false'. Accepts 'false' or 'true'.
73 function wp_register_script( $handle, $src, $deps = array(), $ver = false, $in_footer = false ) {
75 if ( ! is_a( $wp_scripts, 'WP_Scripts' ) ) {
76 if ( ! did_action( 'init' ) )
77 _doing_it_wrong( __FUNCTION__, sprintf( __( 'Scripts and styles should not be registered or enqueued until the %1$s, %2$s, or %3$s hooks.' ),
78 '<code>wp_enqueue_scripts</code>', '<code>admin_enqueue_scripts</code>', '<code>login_enqueue_scripts</code>' ), '3.3' );
79 $wp_scripts = new WP_Scripts();
82 $wp_scripts->add( $handle, $src, $deps, $ver );
84 $wp_scripts->add_data( $handle, 'group', 1 );
90 * Works only if the script has already been added.
92 * Accepts an associative array $l10n and creates a JavaScript object:
101 * @see WP_Dependencies::localize()
102 * @link http://core.trac.wordpress.org/ticket/11520
103 * @global WP_Scripts $wp_scripts The WP_Scripts object for printing scripts.
107 * @param string $handle Script handle the data will be attached to.
108 * @param string $object_name Name for the JavaScript object. Passed directly, so it should be qualified JS variable.
109 * Example: '/[a-zA-Z0-9_]+/'.
110 * @param array $l10n The data itself. The data can be either a single or multi-dimensional array.
111 * @return bool True if the script was successfully localized, false otherwise.
113 function wp_localize_script( $handle, $object_name, $l10n ) {
115 if ( ! is_a( $wp_scripts, 'WP_Scripts' ) ) {
116 if ( ! did_action( 'init' ) )
117 _doing_it_wrong( __FUNCTION__, sprintf( __( 'Scripts and styles should not be registered or enqueued until the %1$s, %2$s, or %3$s hooks.' ),
118 '<code>wp_enqueue_scripts</code>', '<code>admin_enqueue_scripts</code>', '<code>login_enqueue_scripts</code>' ), '3.3' );
123 return $wp_scripts->localize( $handle, $object_name, $l10n );
127 * Remove a registered script.
129 * Note: there are intentional safeguards in place to prevent critical admin scripts,
130 * such as jQuery core, from being unregistered.
132 * @see WP_Dependencies::remove()
133 * @global WP_Scripts $wp_scripts The WP_Scripts object for printing scripts.
137 * @param string $handle Name of the script to be removed.
139 function wp_deregister_script( $handle ) {
141 if ( ! is_a( $wp_scripts, 'WP_Scripts' ) ) {
142 if ( ! did_action( 'init' ) )
143 _doing_it_wrong( __FUNCTION__, sprintf( __( 'Scripts and styles should not be registered or enqueued until the %1$s, %2$s, or %3$s hooks.' ),
144 '<code>wp_enqueue_scripts</code>', '<code>admin_enqueue_scripts</code>', '<code>login_enqueue_scripts</code>' ), '3.3' );
145 $wp_scripts = new WP_Scripts();
149 * Do not allow accidental or negligent de-registering of critical scripts in the admin.
150 * Show minimal remorse if the correct hook is used.
152 $current_filter = current_filter();
153 if ( ( is_admin() && 'admin_enqueue_scripts' !== $current_filter ) ||
154 ( 'wp-login.php' === $GLOBALS['pagenow'] && 'login_enqueue_scripts' !== $current_filter )
157 'jquery', 'jquery-core', 'jquery-migrate', 'jquery-ui-core', 'jquery-ui-accordion',
158 'jquery-ui-autocomplete', 'jquery-ui-button', 'jquery-ui-datepicker', 'jquery-ui-dialog',
159 'jquery-ui-draggable', 'jquery-ui-droppable', 'jquery-ui-menu', 'jquery-ui-mouse',
160 'jquery-ui-position', 'jquery-ui-progressbar', 'jquery-ui-resizable', 'jquery-ui-selectable',
161 'jquery-ui-slider', 'jquery-ui-sortable', 'jquery-ui-spinner', 'jquery-ui-tabs',
162 'jquery-ui-tooltip', 'jquery-ui-widget', 'underscore', 'backbone',
165 if ( in_array( $handle, $no ) ) {
166 $message = sprintf( __( 'Do not deregister the %1$s script in the administration area. To target the frontend theme, use the %2$s hook.' ),
167 "<code>$handle</code>", '<code>wp_enqueue_scripts</code>' );
168 _doing_it_wrong( __FUNCTION__, $message, '3.6' );
173 $wp_scripts->remove( $handle );
179 * Registers the script if $src provided (does NOT overwrite), and enqueues it.
181 * @see WP_Dependencies::add(), WP_Dependencies::add_data(), WP_Dependencies::enqueue()
182 * @global WP_Scripts $wp_scripts The WP_Scripts object for printing scripts.
186 * @param string $handle Name of the script.
187 * @param string|bool $src Path to the script from the root directory of WordPress. Example: '/js/myscript.js'.
188 * @param array $deps An array of registered handles this script depends on. Default empty array.
189 * @param string|bool $ver Optional. String specifying the script version number, if it has one. This parameter
190 * is used to ensure that the correct version is sent to the client regardless of caching,
191 * and so should be included if a version number is available and makes sense for the script.
192 * @param bool $in_footer Optional. Whether to enqueue the script before </head> or before </body>.
193 * Default 'false'. Accepts 'false' or 'true'.
195 function wp_enqueue_script( $handle, $src = false, $deps = array(), $ver = false, $in_footer = false ) {
197 if ( ! is_a( $wp_scripts, 'WP_Scripts' ) ) {
198 if ( ! did_action( 'init' ) )
199 _doing_it_wrong( __FUNCTION__, sprintf( __( 'Scripts and styles should not be registered or enqueued until the %1$s, %2$s, or %3$s hooks.' ),
200 '<code>wp_enqueue_scripts</code>', '<code>admin_enqueue_scripts</code>', '<code>login_enqueue_scripts</code>' ), '3.3' );
201 $wp_scripts = new WP_Scripts();
205 $_handle = explode('?', $handle);
206 $wp_scripts->add( $_handle[0], $src, $deps, $ver );
208 $wp_scripts->add_data( $_handle[0], 'group', 1 );
210 $wp_scripts->enqueue( $handle );
214 * Remove a previously enqueued script.
216 * @see WP_Dependencies::dequeue()
217 * @global WP_Scripts $wp_scripts The WP_Scripts object for printing scripts.
221 * @param string $handle Name of the script to be removed.
223 function wp_dequeue_script( $handle ) {
225 if ( ! is_a( $wp_scripts, 'WP_Scripts' ) ) {
226 if ( ! did_action( 'init' ) )
227 _doing_it_wrong( __FUNCTION__, sprintf( __( 'Scripts and styles should not be registered or enqueued until the %1$s, %2$s, or %3$s hooks.' ),
228 '<code>wp_enqueue_scripts</code>', '<code>admin_enqueue_scripts</code>', '<code>login_enqueue_scripts</code>' ), '3.3' );
229 $wp_scripts = new WP_Scripts();
232 $wp_scripts->dequeue( $handle );
236 * Check whether a script has been added to the queue.
238 * @global WP_Scripts $wp_scripts The WP_Scripts object for printing scripts.
241 * @since 3.5.0 'enqueued' added as an alias of the 'queue' list.
243 * @param string $handle Name of the script.
244 * @param string $list Optional. Status of the script to check. Default 'enqueued'.
245 * Accepts 'enqueued', 'registered', 'queue', 'to_do', and 'done'.
246 * @return bool Whether the script script is queued.
248 function wp_script_is( $handle, $list = 'enqueued' ) {
250 if ( ! is_a( $wp_scripts, 'WP_Scripts' ) ) {
251 if ( ! did_action( 'init' ) )
252 _doing_it_wrong( __FUNCTION__, sprintf( __( 'Scripts and styles should not be registered or enqueued until the %1$s, %2$s, or %3$s hooks.' ),
253 '<code>wp_enqueue_scripts</code>', '<code>admin_enqueue_scripts</code>', '<code>login_enqueue_scripts</code>' ), '3.3' );
254 $wp_scripts = new WP_Scripts();
257 return (bool) $wp_scripts->query( $handle, $list );