You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

WP Rocket特定页面排除JS延迟插件失效求助

问题诊断与修复方案

1. 修复核心语法错误

你的代码中把PHP逻辑运算符&&错误写成了HTML实体&&,这会直接导致页面匹配条件失效,插件无法正确识别目标页面规则。

修改以下两行代码:

( function_exists( 'is_page' ) && is_page( $excluded_slugs ) ) 
|| ( function_exists( 'is_single' ) && is_single( $excluded_slugs ) )

2. 优化页面匹配逻辑

如果你的首页使用自定义模板home-v4.php、tablets页面使用tablet.php,建议同时保留首页判断和模板判断,确保覆盖所有场景:

if ( 
    ( function_exists( 'is_page' ) && is_page( $excluded_slugs ) ) 
    || ( function_exists( 'is_single' ) && is_single( $excluded_slugs ) )
    || is_front_page()
    || is_page_template('home-v4.php')
    || is_page_template('tablet.php')
) {

3. 确保JS脚本匹配准确

WP Rocket的延迟JS排除对匹配字符串要求严格,建议使用更稳定的匹配方式:

  • jQuery直接用脚本句柄jquery(比文件名更可靠,避免文件名版本变化导致匹配失效)
  • Slick保留slick.min.js即可,若失效可尝试其注册句柄slick或slick-js

修改排除脚本部分:

$exclusions[] = 'jquery';
$exclusions[] = 'slick.min.js';
// 若slick仍延迟,替换为句柄:$exclusions[] = 'slick';

4. 强制清除缓存

修改代码后必须彻底清除WP Rocket页面缓存、浏览器缓存,若使用了服务器缓存(如Nginx、Cloudflare)也需同步清除,否则旧缓存会掩盖修改效果。

完整修复后的代码

/**
 * Plugin Name: WP Rocket | Exclude JS scripts from Delay JS only at some URLs
 * Plugin URI: https://github.com/wp-media/wp-rocket-helpers/tree/master/static-files/wp-rocket-static-exclude-delay-js-per-url/
 * Description: Exclude JavaScript files from Delay JS only on specific pages instead of excluding them globally.
 * Version: 1.0
 * Author: WP Rocket Support Team
 * Author URI: https://wp-rocket.me
 * License: GNU General Public License v2 or later
 * License URI: http://www.gnu.org/licenses/gpl-2.0.html
 *
 * Copyright 2021 WP Media <support@wp-rocket.me>
 */

// Namespaces must be declared before any other declaration.
namespace WP_Rocket\Helpers\static_files\exclude\selective_delay_js_exclusions;

// Exit if accessed directly
if ( ! defined( 'ABSPATH' ) ) exit;

function exclude_from_delay( $exclusions ) {

    /**
    This helper will allow you exclude JavaScript files from Delay JS only on specific pages instead of excluding them globally.
    This plugin requires a 2 steps editing.
        
    STEP #1: ON THESE PAGES...
    Change the Slugs ("first-slug", etc, in this example) to the pages where you want to apply these exclusions.
    These are the URLs where the scripts from step 2 won't be delayed.
    For example: On the following pages (STEP #1), don't delay the following OWL Carousel scripts (STEP #2).
    **/
            
    $excluded_slugs = array( 
        'tablets', 
        //'another-slug', 
       // 'another-one', 
    );       
    // STOP EDITING
   
    if ( 
        ( function_exists( 'is_page' ) && is_page( $excluded_slugs ) ) 
        || ( function_exists( 'is_single' ) && is_single( $excluded_slugs ) )
        || is_front_page()
        || is_page_template('home-v4.php')
        || is_page_template('tablet.php')
       
       /* 
       IF YOU NEEED TO EXCLUDE BASED ON DIFFERENT RULES 
        you can replace lines 41 and 42 with different conditionals, or add more conditions, for example:
        || ( is_front_page() )
        || ( is_category() )
        ... etc
        */ 
       
    ) {
        
    /**
    STEP #2: DON'T DELAY THESE SCRIPTS!
    Add the scripts that need to be loaded right away ONLY on the above URLs. 
    To add multiple scripts you can duplicate these lines as meany times as needed.
    **/
    
    $exclusions[] = 'jquery';
    $exclusions[] = 'slick.min.js';
    //$exclusions[] = 'script2.js';
    //$exclusions[] = 'anotherInline';        
    // STOP EDITING

    }

    return $exclusions;

}

add_filter( 'rocket_delay_js_exclusions',  __NAMESPACE__ . '\exclude_from_delay' );

额外排查点

  • 检查是否有其他缓存工具(如CDN、服务器缓存),需同步清除缓存。
  • 确认slick.min.js的实际加载路径,若路径包含目录(如/js/slick.min.js),可尝试用完整路径片段匹配,WP Rocket支持部分字符串匹配。

内容的提问来源于stack exchange,提问作者luna moonfang

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.22 00:00:00