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>init</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>init</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 localizes 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>init</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>init</code>' ), '3.3' );
111 $wp_scripts = new WP_Scripts();
114 $wp_scripts->remove( $handle );
120 * Registers the script if src provided (does NOT overwrite) and enqueues.
123 * @see wp_register_script() For parameter information.
125 function wp_enqueue_script( $handle, $src = false, $deps = array(), $ver = false, $in_footer = false ) {
127 if ( ! is_a( $wp_scripts, 'WP_Scripts' ) ) {
128 if ( ! did_action( 'init' ) )
129 _doing_it_wrong( __FUNCTION__, sprintf( __( 'Scripts and styles should not be registered or enqueued until the %1$s, %2$s, or %3$s hooks.' ),
130 '<code>wp_enqueue_scripts</code>', '<code>admin_enqueue_scripts</code>', '<code>init</code>' ), '3.3' );
131 $wp_scripts = new WP_Scripts();
135 $_handle = explode('?', $handle);
136 $wp_scripts->add( $_handle[0], $src, $deps, $ver );
138 $wp_scripts->add_data( $_handle[0], 'group', 1 );
140 $wp_scripts->enqueue( $handle );
144 * Remove an enqueued script.
147 * @see WP_Scripts::dequeue() For parameter information.
149 function wp_dequeue_script( $handle ) {
151 if ( ! is_a( $wp_scripts, 'WP_Scripts' ) ) {
152 if ( ! did_action( 'init' ) )
153 _doing_it_wrong( __FUNCTION__, sprintf( __( 'Scripts and styles should not be registered or enqueued until the %1$s, %2$s, or %3$s hooks.' ),
154 '<code>wp_enqueue_scripts</code>', '<code>admin_enqueue_scripts</code>', '<code>init</code>' ), '3.3' );
155 $wp_scripts = new WP_Scripts();
158 $wp_scripts->dequeue( $handle );
162 * Check whether script has been added to WordPress Scripts.
164 * By default, checks if the script has been enqueued. You can also
165 * pass 'registered' to $list, to see if the script is registered,
166 * and you can check processing statuses with 'to_do' and 'done'.
168 * @since WP unknown; BP unknown
170 * @param string $handle Name of the script.
171 * @param string $list Optional. Defaults to 'enqueued'. Values are
172 * 'registered', 'enqueued' (or 'queue'), 'to_do', and 'done'.
173 * @return bool Whether script is in the list.
175 function wp_script_is( $handle, $list = 'enqueued' ) {
177 if ( ! is_a( $wp_scripts, 'WP_Scripts' ) ) {
178 if ( ! did_action( 'init' ) )
179 _doing_it_wrong( __FUNCTION__, sprintf( __( 'Scripts and styles should not be registered or enqueued until the %1$s, %2$s, or %3$s hooks.' ),
180 '<code>wp_enqueue_scripts</code>', '<code>admin_enqueue_scripts</code>', '<code>init</code>' ), '3.3' );
181 $wp_scripts = new WP_Scripts();
184 return (bool) $wp_scripts->query( $handle, $list );