我仅在id =“ 15280”的页面中使用了这两个名为“ Everest-tab-lite”和“ easy-testimonials”的插件,但是它们的CSS和JS脚本会在所有页面上加载。为了限制脚本,我尝试了两种方法,但是没有用。我不知道我做错了什么。有什么办法解决这个问题?
第一种方法,我在functions.php文件的末尾添加了以下代码。
function conditionally_load_plugin_js_css(){
if(! is_page( array(15280) ) ){ # Load CSS and JS only on Pages with ID 4 and 12
wp_dequeue_script('easy-testimonials'); # Restrict scripts.
wp_dequeue_style('easy-testimonials'); # Restrict css.
wp_dequeue_script('everest-tab-lite'); # Restrict scripts.
wp_dequeue_style('everest-tab-lite'); # Restrict css.
}
}
add_action( 'wp_enqueue_scripts', 'conditionally_load_plugin_js_css' );
第二种方法,我在functions.php文件的末尾添加了以下代码。
function remove_wpetl_extras() {
remove_action('wp_print_scripts', 'wpetl_enqueue_scripts');
remove_action('wp_print_styles', 'wpetl_enqueue_styles');
}
if( ! is_page('15280') ) {
add_action('wp_head', 'remove_wpetl_extras');
}
我想你只需要检查订单优先级
priority(int)(可选)用于指定与特定操作关联的功能的执行顺序。较低的数字表示较早的执行,并且具有相同优先级的功能按将它们添加到操作的顺序执行。默认值:10个文档
add_action( 'wp_enqueue_scripts', 'conditionally_load_plugin_js_css', 100);
将优先级更改为100
或更高,999
因为大多数插件使用的值较高99
,甚至更高,因此您需要覆盖它。
另外请注意,如果要完全删除样式或脚本,则需要先删除dequeue
脚本deregister
。
编辑:
我看到了问题。我已经测试了代码,并且可以正常工作,但是您必须输入样式/脚本的确切名称。
在您的情况下,两个插件都添加了一些样式表和脚本,Easy Testimonials
添加了6个样式表和2个脚本:
easy_testimonial_style
single-testimonial-block
random-testimonial-block
testimonials-list-block
testimonials-cycle-block
testimonials-grid-block
easy-testimonials-reveal
gp_cycle2 (jquery.cycle2)
并Everest Tab Lite
增加了3个样式表和1个脚本:
et-frontend-style
et_fontawesome_style
et-animate-style
et-frontend-script
请注意,WordPress将自动
-css
在样式表名称的末尾添加。即et-frontend-style
会显示为et-frontend-style-css
因此,您的功能将是:
function conditionally_load_plugin_js_css() {
// Load CSS and JS only on Pages with 15280
if(! is_page( array(15280) ) ) {
// Easy testimonial
wp_dequeue_style('easy_testimonial_style');
wp_dequeue_style('single-testimonial-block');
wp_dequeue_style('random-testimonial-block');
wp_dequeue_style('testimonials-list-block');
wp_dequeue_style('testimonials-cycle-block');
wp_dequeue_style('testimonials-grid-block');
// scripts
wp_deregister_script('gp_cycle2');
wp_deregister_script('easy-testimonials-reveal');
// Everest Tab Lite
wp_dequeue_style('et-frontend-style');
// Keep these if you like fontawesome and animate.css
wp_dequeue_style('et_fontawesome_style');
wp_dequeue_style('et-animate-style');
// script
wp_deregister_script('et-frontend-script');
}
}
// Everest Tab Lite has priority more than 9999 so i changed the priority to 99999
add_action( 'wp_enqueue_scripts', 'conditionally_load_plugin_js_css', 99999);
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句