- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我需要从 wordpress 中删除 jquery ui 脚本(和其他一些脚本)问题是如果我将它们出列并取消注册,那么调用它们的插件将无法工作,因为脚本“丢失”,即使我正在加载所有通过 googleapis 链接使用一个脚本的 jquery ui。
我有以下内容:
add_action('wp_print_scripts','remove_excess_scripts', 100);
function remove_excess_scripts(){
wp_dequeue_script( 'jquery-ui-core' );
wp_dequeue_script( 'jquery-ui-widget' );
wp_dequeue_script( 'jquery-ui-mouse' );
wp_dequeue_script( 'jquery-ui-accordion' );
wp_dequeue_script( 'jquery-ui-autocomplete' );
wp_dequeue_script( 'jquery-ui-slider' );
wp_dequeue_script( 'jquery-ui-progressbar' );
wp_dequeue_script( 'jquery-ui-tabs' );
wp_dequeue_script( 'jquery-ui-sortable' );
wp_dequeue_script( 'jquery-ui-draggable' );
wp_dequeue_script( 'jquery-ui-droppable' );
wp_dequeue_script( 'jquery-ui-selectable' );
wp_dequeue_script( 'jquery-ui-position' );
wp_dequeue_script( 'jquery-ui-datepicker' );
wp_dequeue_script( 'jquery-ui-tooltip' );
wp_dequeue_script( 'jquery-ui-resizable' );
wp_dequeue_script( 'jquery-ui-dialog' );
wp_dequeue_script( 'jquery-ui-button' );
wp_deregister_script( 'jquery-ui-core' );
wp_deregister_script( 'jquery-ui-widget' );
wp_deregister_script( 'jquery-ui-mouse' );
wp_deregister_script( 'jquery-ui-accordion' );
wp_deregister_script( 'jquery-ui-autocomplete' );
wp_deregister_script( 'jquery-ui-slider' );
wp_deregister_script( 'jquery-ui-progressbar' );
wp_deregister_script( 'jquery-ui-tabs' );
wp_deregister_script( 'jquery-ui-sortable' );
wp_deregister_script( 'jquery-ui-draggable' );
wp_deregister_script( 'jquery-ui-droppable' );
wp_deregister_script( 'jquery-ui-selectable' );
wp_deregister_script( 'jquery-ui-position' );
wp_deregister_script( 'jquery-ui-datepicker' );
wp_deregister_script( 'jquery-ui-tooltip' );
wp_deregister_script( 'jquery-ui-resizable' );
wp_deregister_script( 'jquery-ui-dialog' );
wp_deregister_script( 'jquery-ui-button' );
}
加上前面的函数,我正确地注册了 jquery ui 并将其排入队列:
wp_register_script(
'jquery-ui-all',
"http://ajax.googleapis.com/ajax/libs/jqueryui/1.10.3/jquery-ui.min.js",
array('jquery'),
false,
false
);
和
function print_scripts() {
wp_enqueue_script( 'the-bootstrap' );
}
add_action( 'wp_enqueue_scripts', 'print_scripts' );
(其中 the-bootstrap 依赖于 jquery-ui-all)
所有这些都是从 jquery-ui 从 google 加载并且不再输出任何单独的 UI 文件的意义上工作的 - 问题出现是因为插件 WPFullCalendar 将其脚本与一堆 jquery ui 单独的脚本作为依赖项排队我猜他们注销并出队了,因为缺少依赖项,它的脚本没有得到输出?我得到以下错误(由于它的 JS 文件没有被输出)
Uncaught ReferenceError: WPFC is not defined
我如何替换脚本,以便其他插件仍然可以将它们排入队列或依赖于它们,但它们都输出相同的文件。我可以这样做是注销所有名称,然后使用 googleapis 作为源再次注册它们,但是我仍然会收到 15 个不同的 HTTP 请求,用于所有相同的 googleapi 链接...我需要所有这些文件指向 1 googleapi 文件,并且只为一个输出。这可能吗? (并且不应该在其他插件已经成功调用/依赖它们之后将它们从队列中取出 - wp_print_scripts 发生在所有插件入队之后,不是吗?)
更新:
我已经将下面接受的答案修改得更具体一些,以免在依赖性方面破坏其他任何东西。这是正确注销脚本并从任何依赖的脚本中删除它的依赖项的更新代码。确保如果你这样做,你用来替换注销脚本的脚本在 HTML 中加载得足够高(可能是头部),以便依赖它的其他脚本随后出现。
//custom deregister scripts
function georgian_deregister_scripts(){
global $wp_scripts;
//scripts to deregister
$deregisteredscripts = array('jquery-ui-core','jquery-ui-widget','jquery-ui-mouse','jquery-ui-accordion','jquery-ui-autocomplete','jquery-ui-slider','jquery-ui-progressbar' ,'jquery-ui-tabs','jquery-ui-sortable','jquery-ui-draggable','jquery-ui-droppable','jquery-ui-selectable','jquery-ui-position','jquery-ui-datepicker','jquery-ui-tooltip','jquery-ui-resizable','jquery-ui-dialog','jquery-ui-button');
//degregister each script
foreach($deregisteredscripts as $script)
wp_deregister_script( $script );
//remove deregistered scripts as dependencies of any other scripts depending on them
if(false != $wp_scripts->queue)
foreach($wp_scripts->queue as $script)
if(isset($wp_scripts->registered[$script]))
$wp_scripts->registered[$script]->deps = array_diff($wp_scripts->registered[$script]->deps, $deregisteredscripts);
}
add_action('wp_enqueue_scripts', 'georgian_deregister_scripts', 101);
最佳答案
如果您注销脚本,它不会入队,所以您有几个无用的命令。
现在,如何删除对(所有)要排队的脚本的依赖?这个函数应该可以解决问题。
function customize_scripts_output(){
global $wp_scripts;
if(false != $wp_scripts->queue)
foreach($wp_scripts->queue as $script)
if(isset($wp_scripts->registered[$script]))
$wp_scripts->registered[$script]->deps = array();
}
add_action('wp_enqueue_scripts', 'customize_scripts_output', 101);
关于php - 在 wordpress 中替换排队脚本的正确方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20101896/
我想了解 Ruby 方法 methods() 是如何工作的。 我尝试使用“ruby 方法”在 Google 上搜索,但这不是我需要的。 我也看过 ruby-doc.org,但我没有找到这种方法。
Test 方法 对指定的字符串执行一个正则表达式搜索,并返回一个 Boolean 值指示是否找到匹配的模式。 object.Test(string) 参数 object 必选项。总是一个
Replace 方法 替换在正则表达式查找中找到的文本。 object.Replace(string1, string2) 参数 object 必选项。总是一个 RegExp 对象的名称。
Raise 方法 生成运行时错误 object.Raise(number, source, description, helpfile, helpcontext) 参数 object 应为
Execute 方法 对指定的字符串执行正则表达式搜索。 object.Execute(string) 参数 object 必选项。总是一个 RegExp 对象的名称。 string
Clear 方法 清除 Err 对象的所有属性设置。 object.Clear object 应为 Err 对象的名称。 说明 在错误处理后,使用 Clear 显式地清除 Err 对象。此
CopyFile 方法 将一个或多个文件从某位置复制到另一位置。 object.CopyFile source, destination[, overwrite] 参数 object 必选
Copy 方法 将指定的文件或文件夹从某位置复制到另一位置。 object.Copy destination[, overwrite] 参数 object 必选项。应为 File 或 F
Close 方法 关闭打开的 TextStream 文件。 object.Close object 应为 TextStream 对象的名称。 说明 下面例子举例说明如何使用 Close 方
BuildPath 方法 向现有路径后添加名称。 object.BuildPath(path, name) 参数 object 必选项。应为 FileSystemObject 对象的名称
GetFolder 方法 返回与指定的路径中某文件夹相应的 Folder 对象。 object.GetFolder(folderspec) 参数 object 必选项。应为 FileSy
GetFileName 方法 返回指定路径(不是指定驱动器路径部分)的最后一个文件或文件夹。 object.GetFileName(pathspec) 参数 object 必选项。应为
GetFile 方法 返回与指定路径中某文件相应的 File 对象。 object.GetFile(filespec) 参数 object 必选项。应为 FileSystemObject
GetExtensionName 方法 返回字符串,该字符串包含路径最后一个组成部分的扩展名。 object.GetExtensionName(path) 参数 object 必选项。应
GetDriveName 方法 返回包含指定路径中驱动器名的字符串。 object.GetDriveName(path) 参数 object 必选项。应为 FileSystemObjec
GetDrive 方法 返回与指定的路径中驱动器相对应的 Drive 对象。 object.GetDrive drivespec 参数 object 必选项。应为 FileSystemO
GetBaseName 方法 返回字符串,其中包含文件的基本名 (不带扩展名), 或者提供的路径说明中的文件夹。 object.GetBaseName(path) 参数 object 必
GetAbsolutePathName 方法 从提供的指定路径中返回完整且含义明确的路径。 object.GetAbsolutePathName(pathspec) 参数 object
FolderExists 方法 如果指定的文件夹存在,则返回 True;否则返回 False。 object.FolderExists(folderspec) 参数 object 必选项
FileExists 方法 如果指定的文件存在返回 True;否则返回 False。 object.FileExists(filespec) 参数 object 必选项。应为 FileS
我是一名优秀的程序员,十分优秀!