如何在 Elementor (Pro) 中 deregister/dequeue jquery.sticky.js?

How to deregister/dequeue jquery.sticky.js in Elementor (Pro)?

我想通过取消注册不必要的外部资源来提高我的页面速度。我已经设法删除了大部分外部脚本,Elementor 默认在前端加载。但是,我无法以某种方式删除 jQuery 插件 Sticky。我想这与成为 Elementor Pro 的一部分有关。

我已经尝试查看 jQuery 依赖关系,但这对我不起作用。

function remove_jquery_sticky() {
    if ( ! is_admin()) {
        wp_deregister_script( 'sticky' );
    }
}
add_action( 'elementor/frontend/after_register_scripts', 'remove_jquery_sticky' );

I expect the jQuery plugin not to load on the frontend, however it still does.

如果您知道要添加的操作的名称,您可以使用函数 remove_action( $tag, $function_to_remove, $priority ) 或者您可以使用 wp_dequeue_script( $handle )

https://codex.wordpress.org/Function_Reference/remove_action

https://codex.wordpress.org/Function_Reference/wp_dequeue_script

我找到了适合我的解决方案。如果您有更清洁的解决方案,请告诉我:)

    if(is_front_page()) {
        // Dequeue and deregister elementor-sticky
        wp_dequeue_script( 'elementor-sticky' );
        wp_deregister_script( 'elementor-sticky' );

        // Dequeue and deregister elementor-pro-frontend
        wp_dequeue_script( 'elementor-pro-frontend' );
        wp_deregister_script( 'elementor-pro-frontend' );

        // Re-register elementor-frontend without the elementor-sticky dependency.
        $suffix = ( defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ) ? '' : '.min';
        wp_register_script(
                'elementor-pro-frontend',
                ELEMENTOR_PRO_URL . 'assets/js/frontend' . $suffix . '.js',
                [
                    'elementor-frontend-modules',
                ],
                ELEMENTOR_VERSION,
                true
            );
    }
}
add_action( 'wp_enqueue_scripts', 'elementor_pro_frontend_scripts' );```

Elementor 和 Elementor PRO 注册并排队一些具有依赖性的脚本。要删除您需要取消注册并在没有特定脚本的情况下重新注册(例如没有 'elementor-sticky')。

if(is_front_page()) {

        // Dequeue and deregister elementor-pro-frontend
        wp_deregister_script( 'elementor-pro-frontend' );

        // Re-register elementor-frontend without the elementor-sticky dependency.
        $suffix = ( defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ) ? '' : '.min';
        wp_register_script(
                'elementor-pro-frontend',
                ELEMENTOR_PRO_URL . 'assets/js/frontend' . $suffix . '.js',
                [
                    'elementor-frontend-modules',
                ],
                ELEMENTOR_VERSION,
                true
            );
    }
}
add_action( 'wp_enqueue_scripts', 'elementor_pro_frontend_scripts', 20 );