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 string|bool|array $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 https://core.trac.wordpress.org/ticket/11520
103 * @global WP_Scripts $wp_scripts The WP_Scripts object for printing scripts.
107 * @todo Documentation cleanup
109 * @param string $handle Script handle the data will be attached to.
110 * @param string $object_name Name for the JavaScript object. Passed directly, so it should be qualified JS variable.
111 * Example: '/[a-zA-Z0-9_]+/'.
112 * @param array $l10n The data itself. The data can be either a single or multi-dimensional array.
113 * @return bool True if the script was successfully localized, false otherwise.
115 function wp_localize_script( $handle, $object_name, $l10n ) {
117 if ( ! is_a( $wp_scripts, 'WP_Scripts' ) ) {
118 if ( ! did_action( 'init' ) )
119 _doing_it_wrong( __FUNCTION__, sprintf( __( 'Scripts and styles should not be registered or enqueued until the %1$s, %2$s, or %3$s hooks.' ),
120 '<code>wp_enqueue_scripts</code>', '<code>admin_enqueue_scripts</code>', '<code>login_enqueue_scripts</code>' ), '3.3' );
125 return $wp_scripts->localize( $handle, $object_name, $l10n );
129 * Remove a registered script.
131 * Note: there are intentional safeguards in place to prevent critical admin scripts,
132 * such as jQuery core, from being unregistered.
134 * @see WP_Dependencies::remove()
135 * @global WP_Scripts $wp_scripts The WP_Scripts object for printing scripts.
139 * @param string $handle Name of the script to be removed.
141 function wp_deregister_script( $handle ) {
143 if ( ! is_a( $wp_scripts, 'WP_Scripts' ) ) {
144 if ( ! did_action( 'init' ) )
145 _doing_it_wrong( __FUNCTION__, sprintf( __( 'Scripts and styles should not be registered or enqueued until the %1$s, %2$s, or %3$s hooks.' ),
146 '<code>wp_enqueue_scripts</code>', '<code>admin_enqueue_scripts</code>', '<code>login_enqueue_scripts</code>' ), '3.3' );
147 $wp_scripts = new WP_Scripts();
151 * Do not allow accidental or negligent de-registering of critical scripts in the admin.
152 * Show minimal remorse if the correct hook is used.
154 $current_filter = current_filter();
155 if ( ( is_admin() && 'admin_enqueue_scripts' !== $current_filter ) ||
156 ( 'wp-login.php' === $GLOBALS['pagenow'] && 'login_enqueue_scripts' !== $current_filter )
159 'jquery', 'jquery-core', 'jquery-migrate', 'jquery-ui-core', 'jquery-ui-accordion',
160 'jquery-ui-autocomplete', 'jquery-ui-button', 'jquery-ui-datepicker', 'jquery-ui-dialog',
161 'jquery-ui-draggable', 'jquery-ui-droppable', 'jquery-ui-menu', 'jquery-ui-mouse',
162 'jquery-ui-position', 'jquery-ui-progressbar', 'jquery-ui-resizable', 'jquery-ui-selectable',
163 'jquery-ui-slider', 'jquery-ui-sortable', 'jquery-ui-spinner', 'jquery-ui-tabs',
164 'jquery-ui-tooltip', 'jquery-ui-widget', 'underscore', 'backbone',
167 if ( in_array( $handle, $no ) ) {
168 $message = sprintf( __( 'Do not deregister the %1$s script in the administration area. To target the frontend theme, use the %2$s hook.' ),
169 "<code>$handle</code>", '<code>wp_enqueue_scripts</code>' );
170 _doing_it_wrong( __FUNCTION__, $message, '3.6' );
175 $wp_scripts->remove( $handle );
181 * Registers the script if $src provided (does NOT overwrite), and enqueues it.
183 * @see WP_Dependencies::add(), WP_Dependencies::add_data(), WP_Dependencies::enqueue()
184 * @global WP_Scripts $wp_scripts The WP_Scripts object for printing scripts.
188 * @param string $handle Name of the script.
189 * @param string|bool $src Path to the script from the root directory of WordPress. Example: '/js/myscript.js'.
190 * @param array $deps An array of registered handles this script depends on. Default empty array.
191 * @param string|bool $ver Optional. String specifying the script version number, if it has one. This parameter
192 * is used to ensure that the correct version is sent to the client regardless of caching,
193 * and so should be included if a version number is available and makes sense for the script.
194 * @param bool $in_footer Optional. Whether to enqueue the script before </head> or before </body>.
195 * Default 'false'. Accepts 'false' or 'true'.
197 function wp_enqueue_script( $handle, $src = false, $deps = array(), $ver = false, $in_footer = false ) {
199 if ( ! is_a( $wp_scripts, 'WP_Scripts' ) ) {
200 if ( ! did_action( 'init' ) )
201 _doing_it_wrong( __FUNCTION__, sprintf( __( 'Scripts and styles should not be registered or enqueued until the %1$s, %2$s, or %3$s hooks.' ),
202 '<code>wp_enqueue_scripts</code>', '<code>admin_enqueue_scripts</code>', '<code>login_enqueue_scripts</code>' ), '3.3' );
203 $wp_scripts = new WP_Scripts();
207 $_handle = explode('?', $handle);
208 $wp_scripts->add( $_handle[0], $src, $deps, $ver );
210 $wp_scripts->add_data( $_handle[0], 'group', 1 );
212 $wp_scripts->enqueue( $handle );
216 * Remove a previously enqueued script.
218 * @see WP_Dependencies::dequeue()
219 * @global WP_Scripts $wp_scripts The WP_Scripts object for printing scripts.
223 * @param string $handle Name of the script to be removed.
225 function wp_dequeue_script( $handle ) {
227 if ( ! is_a( $wp_scripts, 'WP_Scripts' ) ) {
228 if ( ! did_action( 'init' ) )
229 _doing_it_wrong( __FUNCTION__, sprintf( __( 'Scripts and styles should not be registered or enqueued until the %1$s, %2$s, or %3$s hooks.' ),
230 '<code>wp_enqueue_scripts</code>', '<code>admin_enqueue_scripts</code>', '<code>login_enqueue_scripts</code>' ), '3.3' );
231 $wp_scripts = new WP_Scripts();
234 $wp_scripts->dequeue( $handle );
238 * Check whether a script has been added to the queue.
240 * @global WP_Scripts $wp_scripts The WP_Scripts object for printing scripts.
243 * @since 3.5.0 'enqueued' added as an alias of the 'queue' list.
245 * @param string $handle Name of the script.
246 * @param string $list Optional. Status of the script to check. Default 'enqueued'.
247 * Accepts 'enqueued', 'registered', 'queue', 'to_do', and 'done'.
248 * @return bool Whether the script script is queued.
250 function wp_script_is( $handle, $list = 'enqueued' ) {
252 if ( ! is_a( $wp_scripts, 'WP_Scripts' ) ) {
253 if ( ! did_action( 'init' ) )
254 _doing_it_wrong( __FUNCTION__, sprintf( __( 'Scripts and styles should not be registered or enqueued until the %1$s, %2$s, or %3$s hooks.' ),
255 '<code>wp_enqueue_scripts</code>', '<code>admin_enqueue_scripts</code>', '<code>login_enqueue_scripts</code>' ), '3.3' );
256 $wp_scripts = new WP_Scripts();
259 return (bool) $wp_scripts->query( $handle, $list );