Codex tools: Log in / create account
Languages: English • Русский • (Add your language)
Contents |
A safe way of adding javascripts to a WordPress generated page.
<?php wp_enqueue_script( $handle, $src, $deps, $ver, $in_footer ); ?>
<?php
function my_init_method() {
wp_enqueue_script('scriptaculous');
}
add_action('init', 'my_init_method');
?>
Add and load a new script that depends on scriptaculous (this will also cause it to load scriptaculous into the page as well):
<?php
wp_enqueue_script('newscript',
WP_PLUGIN_URL . '/someplugin/js/newscript.js',
array('scriptaculous'),
'1.0' );
?>
<?php
/*
* This example will work at least on WordPress 2.6.3,
* but maybe on older versions too.
*/
add_action('admin_init', 'my_plugin_admin_init');
add_action('admin_menu', 'my_plugin_admin_menu');
function my_plugin_admin_init()
{
/* Register our script. */
wp_register_script('myPluginScript', WP_PLUGIN_URL . '/myPlugin/script.js');
}
function my_plugin_admin_menu()
{
/* Register our plugin page */
$page = add_submenu_page( 'edit.php',
__('My Plugin', 'myPlugin'),
__('My Plugin', 'myPlugin'), 9, __FILE__,
'my_plugin_manage_menu');
/* Using registered $page handle to hook script load */
add_action('admin_print_scripts-' . $page, 'my_plugin_admin_styles');
}
function my_plugin_admin_styles()
{
/*
* It will be called only on your plugin admin page, enqueue our script here
*/
wp_enqueue_script('myPluginScript');
}
function my_plugin_manage_menu()
{
/* Output our admin page */
}
?>
Note: This function will not work if it is called from a wp_head action, as the <script> tags are output before wp_head runs. Instead, call wp_enqueue_script from an init action function (to load it in all pages), template_redirect (to load it in public pages only), or admin_print_scripts (for admin pages only). Do not use wp_print_scripts (see here for an explanation).
Note: The jQuery library included with WordPress loads in "no conflict" mode. This is to prevent compatibility problems with other javascript libraries that WordPress can load.
In order to use the default jQuery shortcut of $, you can use the following wrapper around your code:
jQuery(document).ready(function($) {
// $() will work as an alias for jQuery() inside of this function
});
That wrapper will cause your code to be executed when the page finishes loading, and the $ will work for calling jQuery. If, for some reason, you want your code to execute immediately (instead of waiting for the DOM ready event), then you can use this wrapper method instead:
(function($) {
// $() will work as an alias for jQuery() inside of this function
})(jQuery);
| Script Name | Handle |
|---|---|
| Scriptaculous Root | scriptaculous-root |
| Scriptaculous Builder | scriptaculous-builder |
| Scriptaculous Drag & Drop | scriptaculous-dragdrop |
| Scriptaculous Effects | scriptaculous-effects |
| Scriptaculous Slider | scriptaculous-slider |
| Scriptaculous Sound | scriptaculous-sound |
| Scriptaculous Controls | scriptaculous-controls |
| Scriptaculous | scriptaculous |
| Image Cropper | cropper |
| SWFUpload | swfupload |
| SWFUpload Degarade | swfupload-degrade |
| SWFUpload Queue | swfupload-queue |
| SWFUpload Handlers | swfupload-handlers |
| jQuery | jquery |
| jQuery Form | jquery-form |
| jQuery Color | jquery-color |
| jQuery UI Core | jquery-ui-core |
| jQuery UI Tabs | jquery-ui-tabs |
| jQuery UI Sortable | jquery-ui-sortable |
| jQuery UI Draggable | jquery-ui-draggable |
| jQuery UI Droppable | jquery-ui-droppable |
| jQuery UI Selectable | jquery-ui-selectable |
| jQuery UI Resizable | jquery-ui-resizable |
| jQuery UI Dialog | jquery-ui-dialog |
| jQuery Interface | interface |
| jQuery Schedule | schedule |
| jQuery Suggest | suggest |
| ThickBox | thickbox |
| jQuery Hotkeys | jquery-hotkeys |
| Simple AJAX Code-Kit | sack |
| QuickTags | quicktags |
| ColorPicker | colorpicker |
| Tiny MCE | tiny_mce |
| Prototype Framework | prototype |
| Autosave | autosave |
| WordPress AJAX Response | wp-ajax-response |
| List Manipulation | wp-lists |
| WP Common | common |
| WP Editor | editor |
| WP Editor Functions | editor-functions |
| AJAX Cat | ajaxcat |
| Admin Categories | admin-categories |
| Admin Tags | admin-tags |
| Admin custom fields | admin-custom-fields |
| Password Strength Meter | password-strength-meter |
| Admin Comments | admin-comments |
| Admin Users | admin-users |
| Admin Forms | admin-forms |
| XFN | xfn |
| Upload | upload |
| PostBox | postbox |
| Slug | slug |
| Post | post |
| Page | page |
| Link | link |
| Comment | comment |
| Admin Gallery | admin-gallery |
| Media Upload | media-upload |
| Admin widgets | admin-widgets |
| Word Count | word-count |
| WP Gears | wp-gears |
| Theme Preview | theme-preview |