说明
将Java脚本添加到WordPress已生成页面的安全方法
用法
<?php wp_enqueue_script( $handle, $src, $deps, $ver, $in_footer ); ?>
参数
$handle
(字符串)(必需)脚本名称。小写字符串。
默认值:None
$src
(字符串)(可选)WordPress根目录下的脚本路径
示例:"/wp-includes/js/scriptaculous/scriptaculous.js"。该参数只在WordPress不了解脚本情况时使用。
默认值:None
$deps
(数组)(可选)脚本所依靠的句柄组成的数组;加载该脚本前需要加载的其它脚本。若没有依赖关系,返回false。该参数只在WordPress不了解脚本情况时使用。
默认值:array()
$ver
(字符串)(可选)指明脚本版本号的字符串(若存在版本号)。默认为false。该参数可确保即使在启用缓存的状态下,发送给客户端的仍然是正确版本,因此如果版本号可用且对脚本有意义,包含该版本号。
默认值:false
$in_footer
(布尔型)(可选)通常情况下脚本会被放置在<head>区块中。如果该函数为true,脚本则会出现在<body>区块的最下方。要求主题在适当的位置中包含有wp_footer()钩子。(WordPress新功能)
默认值:false
示例
加载scriptaculous脚本
<?php
function my_init_method() {
wp_enqueue_script('scriptaculous');
}
add_action('init', my_init_method);
?>
根据scriptaculous加载脚本
添加并加载一个依赖于scriptaculous的脚本(这将使scriptaculous也被加载入页面):
<?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 */
}
?>
注意:如果是来自wp_head动作的调用,wp_enqueue_script函数将不起作用,因为在wp_head运行前,<script>标签就已经输出了。应该从init动作函数(在所有页面中加载脚本)、template_redirect (只在公共页面加载脚本)或admin_print_scripts (只在管理页面加载脚本)中调用wp_enqueue_script。不要使用wp_print_scripts(参见此说明)。
WordPress中的默认脚本
注意:WordPress中的jQuery库会以“无冲突”方式加载。利用以下代码按正常方式使用$function:
jQuery(document).ready(function($) {
// $() will work as an alias for jQuery() inside of this function
});
脚本名称 | 句柄 |
---|---|
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 Interface | interface |
jQuery Schedule | schedule |
jQuery Suggest | suggest |
ThickBox | thickbox |
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 |
分类:中文手册