3 * BackPress script procedural API.
10 * Prints script tags in document head.
12 * Called by admin-header.php and by wp_head hook. Since it is called by wp_head
13 * on every page load, the function does not instantiate the WP_Scripts object
14 * unless script names are explicitly passed. Does make use of already
15 * instantiated $wp_scripts if present. Use provided wp_print_scripts hook to
16 * register/enqueue new scripts.
19 * @see WP_Dependencies::print_scripts()
21 function wp_print_scripts( $handles = false ) {
22 do_action( 'wp_print_scripts' );
23 if ( '' === $handles ) // for wp_head
27 if ( ! is_a( $wp_scripts, 'WP_Scripts' ) ) {
28 if ( ! did_action( 'init' ) )
29 _doing_it_wrong( __FUNCTION__, sprintf( __( 'Scripts and styles should not be registered or enqueued until the %1$s, %2$s, or %3$s hooks.' ),
30 '<code>wp_enqueue_scripts</code>', '<code>admin_enqueue_scripts</code>', '<code>login_enqueue_scripts</code>' ), '3.3' );
33 return array(); // No need to instantiate if nothing is there.
35 $wp_scripts = new WP_Scripts();
38 return $wp_scripts->do_items( $handles );
42 * Register new Javascript file.
45 * @param string $handle Script name
46 * @param string $src Script url
47 * @param array $deps (optional) Array of script names on which this script depends
48 * @param string|bool $ver (optional) Script version (used for cache busting), set to null to disable
49 * @param bool $in_footer (optional) Whether to enqueue the script before </head> or before </body>
52 function wp_register_script( $handle, $src, $deps = array(), $ver = false, $in_footer = false ) {
54 if ( ! is_a( $wp_scripts, 'WP_Scripts' ) ) {
55 if ( ! did_action( 'init' ) )
56 _doing_it_wrong( __FUNCTION__, sprintf( __( 'Scripts and styles should not be registered or enqueued until the %1$s, %2$s, or %3$s hooks.' ),
57 '<code>wp_enqueue_scripts</code>', '<code>admin_enqueue_scripts</code>', '<code>login_enqueue_scripts</code>' ), '3.3' );
58 $wp_scripts = new WP_Scripts();
61 $wp_scripts->add( $handle, $src, $deps, $ver );
63 $wp_scripts->add_data( $handle, 'group', 1 );
67 * Wrapper for $wp_scripts->localize().
69 * Used to localize a script.
70 * Works only if the script has already been added.
71 * Accepts an associative array $l10n and creates JS object:
77 * See http://core.trac.wordpress.org/ticket/11520 for more information.
81 * @param string $handle The script handle that was registered or used in script-loader
82 * @param string $object_name Name for the created JS object. This is passed directly so it should be qualified JS variable /[a-zA-Z0-9_]+/
83 * @param array $l10n Associative PHP array containing the translated strings. HTML entities will be converted and the array will be JSON encoded.
84 * @return bool Whether the localization was added successfully.
86 function wp_localize_script( $handle, $object_name, $l10n ) {
88 if ( ! is_a( $wp_scripts, 'WP_Scripts' ) ) {
89 if ( ! did_action( 'init' ) )
90 _doing_it_wrong( __FUNCTION__, sprintf( __( 'Scripts and styles should not be registered or enqueued until the %1$s, %2$s, or %3$s hooks.' ),
91 '<code>wp_enqueue_scripts</code>', '<code>admin_enqueue_scripts</code>', '<code>login_enqueue_scripts</code>' ), '3.3' );
96 return $wp_scripts->localize( $handle, $object_name, $l10n );
100 * Remove a registered script.
103 * @see WP_Scripts::remove() For parameter information.
105 function wp_deregister_script( $handle ) {
107 if ( ! is_a( $wp_scripts, 'WP_Scripts' ) ) {
108 if ( ! did_action( 'init' ) )
109 _doing_it_wrong( __FUNCTION__, sprintf( __( 'Scripts and styles should not be registered or enqueued until the %1$s, %2$s, or %3$s hooks.' ),
110 '<code>wp_enqueue_scripts</code>', '<code>admin_enqueue_scripts</code>', '<code>login_enqueue_scripts</code>' ), '3.3' );
111 $wp_scripts = new WP_Scripts();
114 // Do not allow accidental or negligent deregistering of critical scripts in the admin. Show minimal remorse if the correct hook is used.
115 if ( is_admin() && 'admin_enqueue_scripts' !== current_filter() ) {
117 'jquery', 'jquery-core', 'jquery-migrate', 'jquery-ui-core', 'jquery-ui-accordion',
118 'jquery-ui-autocomplete', 'jquery-ui-button', 'jquery-ui-datepicker', 'jquery-ui-dialog',
119 'jquery-ui-draggable', 'jquery-ui-droppable', 'jquery-ui-menu', 'jquery-ui-mouse',
120 'jquery-ui-position', 'jquery-ui-progressbar', 'jquery-ui-resizable', 'jquery-ui-selectable',
121 'jquery-ui-slider', 'jquery-ui-sortable', 'jquery-ui-spinner', 'jquery-ui-tabs',
122 'jquery-ui-tooltip', 'jquery-ui-widget',
123 'underscore', 'backbone',
126 if ( in_array( $handle, $no ) ) {
127 $message = sprintf( __( 'Do not deregister the %1$s script in the administration area. To target the frontend theme, use the %2$s hook.' ),
128 "<code>$handle</code>", '<code>wp_enqueue_scripts</code>' );
129 _doing_it_wrong( __FUNCTION__, $message, '3.6' );
134 $wp_scripts->remove( $handle );
140 * Registers the script if src provided (does NOT overwrite) and enqueues.
143 * @see wp_register_script() For parameter information.
145 function wp_enqueue_script( $handle, $src = false, $deps = array(), $ver = false, $in_footer = false ) {
147 if ( ! is_a( $wp_scripts, 'WP_Scripts' ) ) {
148 if ( ! did_action( 'init' ) )
149 _doing_it_wrong( __FUNCTION__, sprintf( __( 'Scripts and styles should not be registered or enqueued until the %1$s, %2$s, or %3$s hooks.' ),
150 '<code>wp_enqueue_scripts</code>', '<code>admin_enqueue_scripts</code>', '<code>login_enqueue_scripts</code>' ), '3.3' );
151 $wp_scripts = new WP_Scripts();
155 $_handle = explode('?', $handle);
156 $wp_scripts->add( $_handle[0], $src, $deps, $ver );
158 $wp_scripts->add_data( $_handle[0], 'group', 1 );
160 $wp_scripts->enqueue( $handle );
164 * Remove an enqueued script.
167 * @see WP_Scripts::dequeue() For parameter information.
169 function wp_dequeue_script( $handle ) {
171 if ( ! is_a( $wp_scripts, 'WP_Scripts' ) ) {
172 if ( ! did_action( 'init' ) )
173 _doing_it_wrong( __FUNCTION__, sprintf( __( 'Scripts and styles should not be registered or enqueued until the %1$s, %2$s, or %3$s hooks.' ),
174 '<code>wp_enqueue_scripts</code>', '<code>admin_enqueue_scripts</code>', '<code>login_enqueue_scripts</code>' ), '3.3' );
175 $wp_scripts = new WP_Scripts();
178 $wp_scripts->dequeue( $handle );
182 * Check whether script has been added to WordPress Scripts.
184 * By default, checks if the script has been enqueued. You can also
185 * pass 'registered' to $list, to see if the script is registered,
186 * and you can check processing statuses with 'to_do' and 'done'.
188 * @since WP unknown; BP unknown
190 * @param string $handle Name of the script.
191 * @param string $list Optional. Defaults to 'enqueued'. Values are
192 * 'registered', 'enqueued' (or 'queue'), 'to_do', and 'done'.
193 * @return bool Whether script is in the list.
195 function wp_script_is( $handle, $list = 'enqueued' ) {
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();
204 return (bool) $wp_scripts->query( $handle, $list );