WordPress函数:wp_register_script 注册 javascript 脚本

江河/ 2023年08月07日/ WordPress/ 浏览 1660

函数原型:


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


注册 javascript 脚本。


WordPress 为了解决 javascript 依赖和版本更新的问题,提供了一组 API。


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


如果已注册脚本的句柄列在使用wp_enque_script()入队的另一个脚本的$deps依赖项数组中,则该脚本将在加载入队脚本之前自动加载。这大大简化了确保脚本具有所需的所有依赖项的过程。


因此,register函数的主要目的是,如果将相同的脚本或样式放入多个代码段中,则无需重复代码,从而简化代码。


如果要在站点前端调用该函数,则应使用wp_enque_scripts或init操作挂钩来调用该函数。要在管理屏幕上调用它,请使用admin_enque_scripts操作挂钩。对于登录屏幕,请使用login_enqueue_scripts操作挂钩。在操作挂钩之外调用它通常会导致意外的结果,应该避免。


如果尝试使用不同的参数注册或排队已注册的句柄,则会忽略新参数。相反,使用wp_deregister_script()并使用新参数再次注册脚本。


参数说明:


$handle,脚本的名称。应该是唯一的。


$src,脚本的完整URL,或相对于WordPress根目录的脚本路径。若source设置为false,那个么script就是它所依赖的其他脚本的别名。


$deps,已注册脚本的数组处理此脚本所依赖的。


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


$in_footer,是否在</body>之前而不是在<head>中对脚本进行排队。


在 WordPress 中已注册的 javascript:


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


函数源码:


function wp_register_script( $handle, $src, $deps = array(), $ver = false, $in_footer = false ) {
	_wp_scripts_maybe_doing_it_wrong( __FUNCTION__, $handle );

	$wp_scripts = wp_scripts();

	$registered = $wp_scripts->add( $handle, $src, $deps, $ver );
	if ( $in_footer ) {
		$wp_scripts->add_data( $handle, 'group', 1 );
	}

	return $registered;
}


使用举例:


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


发表评论

暂无评论,抢个沙发...

客服 工单