Conditionally dequeue dependency of scripts


Please note that, I’m not telling about conditionally enqueue/dequeue scripts, I’m referring to conditionally dequeue certain dependencies from an existing enqueued scripts in front end. And please note the question is not plugin-specific. I want to be enlightened about the general rules.

I used Elementor (page builder) for front end. But it’s not used in all my pages. So the default lightbox feature enabled in Elementor doesn’t work in those pages. That’s why I added my own lightbox throughout the site, using a regex $pattern ="/<a(.*?)href=('|")(.*?).(bmp|gif|jpeg|jpg|png)('|")(.*?)>/i"; hooked to the the_content filter. Thing’s working fine, but-

Where there the Elementor gallery or lightbox is triggered, both the scripts are messing up. Two lightbox are being called, sometimes the gallery lightbox are conflicting and not working etc.

Solution 1:

I know I can conditionally filter the_content with the regex like:

if( ! is_singular('cpt') ) {
    return $content = preg_replace($pattern, $replacement, $content);

But I actually want to keep this feature everywhere. Just want to remove the elementor lightbox where I want ’em to. So, I’s trying the solution#2.

Solution 2:

What Elementor did, is something, like:

wp_enqueue_script('elementor-frontend', 'link/to/frontend.js', array('elementor-dialog', 'elementor-waypoints', 'jquery-swiper'), '1.9.7', true);

Now I want not to load the lightbox feature from Elementor, so I tried to dequeue the 'elementor-dialog':

if( is_singular('cpt') ) {
    wp_dequeue_script( 'elementor-dialog' );

But it’s not working, as the dependencies are set in 'elementor-frontend'. So I tried deregistering dialog:

if( is_singular('cpt') ) {
    wp_dequeue_script( 'elementor-dialog' );
    wp_deregister_script( 'elementor-dialog' );

It’s devastating, because it dequeued the frontend.js.

How can I change the dependencies of frontend.js (handle: 'elementor-frontend') on the fly, so that I can change the dependencies to array('elementor-waypoints', 'jquery-swiper')?

Mayeenul Islam 2 months 0 Answers 7 views 0

Leave an answer