当前浏览:首页 / WordPress函数 / wp_register_script()

wp_register_script( string $handle, string|bool $src, string[] $deps = array(), string|bool|null $ver = false, bool $in_footer = false ): bool

注册新script脚本

registermore...

scriptmore...


描述

注册稍后要使用wp_enqueue_script()函数加入队列的脚本。

另见

  • WP_Dependencies::add()
  • WP_Dependencies::add_data()

参数

$handlestring必填
脚本的名称(句柄),应该是唯一的。
$srcstring|bool必填
脚本的完整URL,或脚本相对于WordPress根目录的路径。
如果源设置为false,则脚本是它所依赖的其他脚本的别名。
$depsstring[]可选
这个脚本所依赖的已注册的脚本句柄的数组。

默认:array()

$verstring|bool|null可选
指定脚本版本号(如果有)的字符串,该脚本版本号作为查询字符串添加到URL中,以用于缓存破坏。如果版本设置为false,则会自动添加与当前安装的WordPress版本相等的版本号。
如果设置为null,则不添加版本。

默认:false

$in_footerbool可选
是否将脚本列队在</body>之前,而不是在<head>中。
默认'false'

默认:false


返回

bool 脚本是否已注册。成功时为true,失败时为false。


更多信息

使用wp_register_script()预注册的脚本,如果被列为另一个列队脚本的依赖项,则不需要使用wp_enqueue_script() 手动排队。WordPress将自动引入注册的脚本,然后再包含将注册脚本的句柄作为依赖项列出的排队脚本。

用法

wp_register_script( $handle, $src, $deps, $ver, $in_footer );

  • 注册脚本在技术上是不必要的,但强烈建议您这样做。
  • 如果注册脚本的句柄列在用wp_enqueue_script()排队的另一个脚本的$deps依赖项数组中,则该脚本将在加载排队脚本之前自动加载。这大大简化了确保脚本具有所需的所有依赖项的过程。下面是一个简单的例子。
  • 因此,注册函数的主要目的是,如果在多个代码段中加入相同的脚本或样式,则可以通过消除重复代码的需要来简化代码。这样做的好处很多,可能不需要在这里列出。

注意

  • 如果您想在站点前端调用它,应使用wp_enqueue_scriptsinit动作钩子。要在管理界面上调用它,请使用admin_enqueue_scripts动作钩子。对于登录界面,请使用login_enqueue_scripts动作钩子。在动作挂钩之外调用它通常会导致意外的结果,应该避免。
  • 如果尝试用不同的参数注册或排队已注册的句柄,新参数将被忽略。请使用wp_deregister_script()并使用新参数再次注册脚本。
  • jQuery UI Effects 不包含在jquery-ui-core句柄中

核心注册脚本

默认情况下,除了core本身使用的脚本外,WordPress还捆绑了许多web开发人员常用的流行脚本。下面是这些脚本的句柄和路径的列表(不完整)。

句柄(名称) 在WordPress中的路径
utils /wp-includes/js/utils.js
common /wp-admin/js/common.js
sack /wp-includes/js/tw-sack.js
quicktags /wp-includes/js/quicktags.js
colorpicker /wp-includes/js/colorpicker.js
editor /wp-admin/js/editor.js
wp-fullscreen /wp-admin/js/wp-fullscreen.js
wp-ajax-response /wp-includes/js/wp-ajax-response.js
wp-pointer /wp-includes/js/wp-pointer.js
autosave /wp-includes/js/autosave.js
heartbeat /wp-includes/js/heartbeat.js
wp-auth-check /wp-includes/js/wp-auth-check.js
wp-lists /wp-includes/js/wp-lists.js
prototype external: //ajax.googleapis.com/ajax/libs/prototype/1.7.1.0/prototype.js
scriptaculous-root external: //ajax.googleapis.com/ajax/libs/prototype/1.7.1.0/prototype.js
scriptaculous-builder external: //ajax.googleapis.com/ajax/libs/prototype/1.7.1.0/builder.js
scriptaculous-dragdrop external: //ajax.googleapis.com/ajax/libs/prototype/1.7.1.0/dragdrop.js
scriptaculous-effects external: //ajax.googleapis.com/ajax/libs/prototype/1.7.1.0/effects.js
scriptaculous-slider external: //ajax.googleapis.com/ajax/libs/prototype/1.7.1.0/slider.js
scriptaculous-sound external: //ajax.googleapis.com/ajax/libs/prototype/1.7.1.0/sound.js
scriptaculous-controls external: //ajax.googleapis.com/ajax/libs/prototype/1.7.1.0/controls.js
scriptaculous scriptaculous-dragdrop, scriptaculous-slider, scriptaculous-controls
cropper /wp-includes/js/crop/cropper.js
jquery (v1.10.2 as of WP 3.8) jquery-core, jquery-migrate
jquery-core /wp-includes/js/jquery/jquery.js
jquery-migrate /wp-includes/js/jquery/jquery-migrate.js (v1.10.2 as of WP 3.8)
jquery-ui-core /wp-includes/js/jquery/ui/jquery.ui.core.min.js
jquery-effects-core /wp-includes/js/jquery/ui/jquery.ui.effect.min.js
jquery-effects-blind /wp-includes/js/jquery/ui/jquery.ui.effect-blind.min.js
jquery-effects-bounce /wp-includes/js/jquery/ui/jquery.ui.effect-bounce.min.js
jquery-effects-clip /wp-includes/js/jquery/ui/jquery.ui.effect-clip.min.js
jquery-effects-drop /wp-includes/js/jquery/ui/jquery.ui.effect-drop.min.js
jquery-effects-explode /wp-includes/js/jquery/ui/jquery.ui.effect-explode.min.js
jquery-effects-fade /wp-includes/js/jquery/ui/jquery.ui.effect-fade.min.js
jquery-effects-fold /wp-includes/js/jquery/ui/jquery.ui.effect-fold.min.js
jquery-effects-highlight /wp-includes/js/jquery/ui/jquery.ui.effect-highlight.min.js
jquery-effects-pulsate /wp-includes/js/jquery/ui/jquery.ui.effect-pulsate.min.js
jquery-effects-scale /wp-includes/js/jquery/ui/jquery.ui.effect-scale.min.js
jquery-effects-shake /wp-includes/js/jquery/ui/jquery.ui.effect-shake.min.js
jquery-effects-slide /wp-includes/js/jquery/ui/jquery.ui.effect-slide.min.js
jquery-effects-transfer /wp-includes/js/jquery/ui/jquery.ui.effect-transfer.min.js
jquery-ui-accordion /wp-includes/js/jquery/ui/jquery.ui.accordion.min.js
jquery-ui-autocomplete /wp-includes/js/jquery/ui/jquery.ui.autocomplete.min.js
jquery-ui-button /wp-includes/js/jquery/ui/jquery.ui.button.min.js
jquery-ui-datepicker /wp-includes/js/jquery/ui/jquery.ui.datepicker.min.js
jquery-ui-dialog /wp-includes/js/jquery/ui/jquery.ui.dialog.min.js
jquery-ui-draggable /wp-includes/js/jquery/ui/jquery.ui.draggable.min.js
jquery-ui-droppable /wp-includes/js/jquery/ui/jquery.ui.droppable.min.js
jquery-ui-menu /wp-includes/js/jquery/ui/jquery.ui.menu.min.js
jquery-ui-mouse /wp-includes/js/jquery/ui/jquery.ui.mouse.min.js
jquery-ui-position /wp-includes/js/jquery/ui/jquery.ui.position.min.js
jquery-ui-progressbar /wp-includes/js/jquery/ui/jquery.ui.progressbar.min.js
jquery-ui-resizable /wp-includes/js/jquery/ui/jquery.ui.resizable.min.js
jquery-ui-selectable /wp-includes/js/jquery/ui/jquery.ui.selectable.min.js
jquery-ui-slider /wp-includes/js/jquery/ui/jquery.ui.slider.min.js
jquery-ui-sortable /wp-includes/js/jquery/ui/jquery.ui.sortable.min.js
jquery-ui-spinner /wp-includes/js/jquery/ui/jquery.ui.spinner.min.js
jquery-ui-tabs /wp-includes/js/jquery/ui/jquery.ui.tabs.min.js
jquery-ui-tooltip /wp-includes/js/jquery/ui/jquery.ui.tooltip.min.js
jquery-ui-widget /wp-includes/js/jquery/ui/jquery.ui.widget.min.js
jquery-form /wp-includes/js/jquery/jquery.form.js
jquery-color /wp-includes/js/jquery/jquery.color.min.js
suggest /wp-includes/js/jquery/suggest.js
schedule /wp-includes/js/jquery/jquery.schedule.js
jquery-query /wp-includes/js/jquery/jquery.query.js
jquery-serialize-object /wp-includes/js/jquery/jquery.serialize-object.js
jquery-hotkeys /wp-includes/js/jquery/jquery.hotkeys.js
jquery-table-hotkeys /wp-includes/js/jquery/jquery.table-hotkeys.js
jquery-touch-punch /wp-includes/js/jquery/jquery.ui.touch-punch.js
jquery-masonry /wp-includes/js/jquery/jquery.masonry.min.js
thickbox /wp-includes/js/thickbox/thickbox.js
jcrop /wp-includes/js/jcrop/jquery.Jcrop.js
swfobject /wp-includes/js/swfobject.js
plupload /wp-includes/js/plupload/plupload.js
plupload-html5 wp-includes/js/plupload/plupload.html5.js
plupload-flash /wp-includes/js/plupload/plupload.flash.js
plupload-silverlight /wp-includes/js/plupload/plupload.silverlight.js
plupload-html4 /wp-includes/js/plupload/plupload.html4.js
plupload-all plupload, plupload-html5, plupload-flash, plupload-silverlight, plupload-html4
plupload-handlers /wp-includes/js/plupload/handlers.js
wp-plupload /wp-includes/js/plupload/wp-plupload.js
swfupload /wp-includes/js/swfupload/swfupload.js
swfupload-swfobject /wp-includes/js/swfupload/plugins/swfupload.swfobject.js
swfupload-queue /wp-includes/js/swfupload/plugins/swfupload.queue.js
swfupload-speed /wp-includes/js/swfupload/plugins/swfupload.speed.js
swfupload-all /wp-includes/js/swfupload/swfupload-all.js
swfupload-handlers /wp-includes/js/swfupload/handlers.js
comment-reply /wp-includes/js/comment-reply.js
json2 /wp-includes/js/json2.js
underscore /wp-includes/js/underscore.min.js
backbone /wp-includes/js/backbone.min.js
wp-util /wp-includes/js/wp-util.js
wp-backbone /wp-includes/js/wp-backbone.js
revisions /wp-admin/js/revisions.js
imgareaselect /wp-includes/js/imgareaselect/jquery.imgareaselect.js
mediaelement /wp-includes/js/mediaelement/mediaelement-and-player.min.js
wp-mediaelement /wp-includes/js/mediaelement/wp-mediaelement.js
zxcvbn-async /wp-includes/js/zxcvbn-async.js
password-strength-meter /wp-admin/js/password-strength-meter.js
user-profile /wp-admin/js/user-profile.js
user-suggest /wp-admin/js/user-suggest.js
admin-bar /wp-includes/js/admin-bar.js
wplink /wp-includes/js/wplink.js
wpdialogs /wp-includes/js/tinymce/plugins/wpdialogs/js/wpdialog.js
wpdialogs-popup /wp-includes/js/tinymce/plugins/wpdialogs/js/popup.js
word-count /wp-admin/js/word-count.js
media-upload /wp-admin/js/media-upload.js
hoverIntent /wp-includes/js/hoverIntent.js
customize-base /wp-includes/js/customize-base.js
customize-loader
customize-preview
customize-controls
accordion
shortcode
media-models
media-views
media-editor
mce-view
admin-tags
admin-comments
xfn
postbox
post
link
comment
admin-gallery
admin-widgets
theme
theme-install
inline-edit-post
inline-edit-tax
plugin-install
farbtastic
iris
wp-color-picker
dashboard
list-revisions
media
image-edit
set-post-thumbnail
nav-menu
custom-header
custom-background
media-gallery
svg-painter


源码

查看源码 官方文档


更新日志

版本描述
4.3.0已添加返回值。
2.1.0开始引入

使用示例

  • 示例1

    链接到外部JS库并在项目中轻松使用的简单方法:
    注册:

     wp_register_script( 'someScript-js', 'https://domain.com/someScript.js' , '', '', true );

    这将把someScript.js放在HTML文件底部
    调用:

    wp_enqueue_script( 'someScript-js' );
  • 示例2

    Luizaris更正的代码:

    add_action("wp_enqueue_scripts", "myscripts");
    function myscripts() { 
        wp_register_script('myfirstscript', 
                            get_template_directory_uri() .'/myscript.js',   //
                            array ('jquery', 'jquery-ui'),					//depends on these, however, they are registered by core already, so no need to enqueue them.
                            false, false);
        wp_enqueue_script('myfirstscript');
         
    }
  • 示例3

    在仅当加载另一个脚本时才应加载第一个脚本的情况下:

    function my_enqueue_scripts()
    {
        wp_register_script( 'first', get_template_directory_uri() . 'js/first.js' );
    
        wp_enqueue_script( 'second', get_template_directory_uri() . 'js/second.js', array( 'first' ) );
    }
    add_action( 'wp_enqueue_scripts', 'my_enqueue_scripts' );

    这里,只有当second.js为列队/加载时,才会加载first.js(在second.js之前)。

  • 示例4

    使用register_script()添加脚本及其依赖项

    function myscripts() {
        //get some external script that is needed for this script
        wp_enqueue_script('jquery-ui', 'http://ajax.googleapis.com/ajax/libs/jqueryui/1.9.1/jquery-ui.min.js'); 
        $script = get_template_directory_uri() . '/library/myscript.js';
        wp_register_script('myfirstscript', 
                            $script, 
                            array ('jquery', 'jquery-ui'), 
                            false, false);
        //always enqueue the script after registering or nothing will happen
        wp_enqueue_script('fullpage-slimscroll');
        
    }
    add_action("wp_enqueue_scripts", "myscripts");