Detecting when Divi Builder is Activated / Deactivated

Written by Dan Mossop

In my earlier post, I described how to detect if the Divi Builder is in use on a given post / page edit screen. But what if you want to actually detect when the user enables / disables Divi Builder editor? It can be done as in the following way.

Adding jQuery to the Post / Page Edit Screen

First we need a way to add in our jQuery code to the post / page edit screen. We can do so using the following construct:

function dfd_example() { ?>
<script>
jQuery(function($){	
    // Our jQuery code will go here
});
</script>
<?php
}
add_action('admin_head-post.php', 'dfd_example');
add_action('admin_head-post-new.php', 'dfd_example');

This uses the WordPress hooks "admin_head-post.php" and "admin_head-post-new.php" to call our dfd_example() function whenever the post / page edit screen or new post / page screen is displayed. Our function, when called, outputs a script tag within the html head and then starts a jQuery code block.

Triggering an Event when the Divi Builder is Enabled

Next, we will use the following jQuery to detect clicks on the Divi Builder button and, if appropriate, trigger a custom "diviBuilderEnabled" event to signify that the Divi Builder has been enabled:

// Trigger "diviBuilderEnabled" event when Divi Builder editor enabled
$('#et_pb_toggle_builder').click(function(){
    if (!$('#et_pb_toggle_builder.et_pb_builder_is_used').length) { 
        $(document).trigger("diviBuilderEnabled");
    } 
});

This attaches a click handler to the "Use Divi Builder" button. When it is invoked, it checks to make sure that the Divi Builder was not already running, using a variant of the code from my previous post. If it was not running, then we assume that it is now about to start running and trigger the "diviBuilderEnabled" event.

Triggering and Event when the Divi Builder is Disabled

In a somewhat similar way, we can trigger an event when the Divi Builder is disabled by the user:

// Trigger "diviBuilderDisabled" event when Divi Builder editor disabled
$(document).on('click', '[data-action="deactivate_builder"] .et_pb_prompt_proceed', function(){		
    $(document).trigger("diviBuilderDisabled");
});

You'll notice, that while looking somewhat similar to the last block of code, there are some notable differences. When checking that the user has disabled the Builder, we can't simply check if the Divi Builder button has been clicked as we did before. This is because Divi pops up a confirmation prompt when disabling the Divi Builder. Only if the prompt is accepted is the Builder actually disabled. So we use the '[data-action="deactivate_builder"] .et_pb_prompt_proceed' selector to detect clicks on the prompt acceptance button. As the prompt is dynamically created and doesn't exist in the page at the time we add our code, we can't use "click()" to register the handler. Instead we need to use the "on" function to attach the click handler to the document, which will capture the click event as it bubbles up the DOM hierarchy.

Performing Actions when the Divi Builder is Enabled / Disabled

Now that we are triggering events when the Builder is enabled / disabled, we need a way to actually run code when those events occur. This can be again done using the "on" function to bind a function to these specific events, like so:

// log divi builder enabled events	
$(document).on("diviBuilderEnabled", function(){
    console.log('Divi Builder enabled');
});
	
// log divi builder disabled events
$(document).on("diviBuilderDisabled", function(){
    console.log('Divi Builder disabled');
});

This example will simply log a message to the browser's JavaScript console each time one of the events occurs.

Putting it All Together

Here's the final code:

function dfd_example() { ?>
<script>
jQuery(function($){
	
    // Trigger "diviBuilderEnabled" event when Divi Builder editor enabled
    $('#et_pb_toggle_builder').click(function(){
        if (!$('#et_pb_toggle_builder.et_pb_builder_is_used').length) { 
            $(document).trigger("diviBuilderEnabled");
        } 
    });
	
    // Trigger "diviBuilderDisabled" event when Divi Builder editor disabled
    $(document).on('click', '[data-action="deactivate_builder"] .et_pb_prompt_proceed', function(){		
        $(document).trigger("diviBuilderDisabled");
    });
	
    // log divi builder enabled events	
    $(document).on("diviBuilderEnabled", function(){
        console.log('Divi Builder enabled');
    });
	
    // log divi builder disabled events
    $(document).on("diviBuilderDisabled", function(){
        console.log('Divi Builder disabled');
    });
	
});
</script>
<?php
}
add_action('admin_head-post.php', 'dfd_example');
add_action('admin_head-post-new.php', 'dfd_example');

Boost Your Post Engagement with WP Magic CTAs

Maximize your WordPress post's impact with the WP Magic CTAs plugin. This AI-powered tool generates targeted calls-to-action, elevating your post's engagement and conversion rates effortlessly. Just what you need for directing traffic and promoting offers directly from your posts!

About Dan Mossop

Dan is a Scottish-born web developer, now living in Brisbane with his wife and son. He has been sharing tips and helping users with Divi since 2014. He created Divi Booster, the first Divi plugin, and continues to develop it along with 20+ other Divi plugins. Dan has a PhD in Computer Science, a background in web security and likes a lot of stuff, 

1 Comment

  1. this was very usefull thank you

    Reply

Submit a Comment

Comments are manually moderated and approved at the time they are answered. A preview is shown while pending but may disappear if your are cookies cleared - don't worry though, the comment is still in the queue.

Your email address will not be published. Required fields are marked *.

We may earn a commission when you visit links on our website.

Latest Posts

Set Custom CSS IDs for Individual Divi Accordion Items

Assigning unique CSS IDs to specific Divi Accordion items allows for precise control over styling, targeting, and linking within your page content. This ability is particularly useful when you want to apply custom designs or create anchor links to particular accordion...

Enable Swipe Navigation in the Divi Gallery Lightbox

Enabling swipe navigation in the Divi Gallery module's lightbox allows users to seamlessly browse through gallery images by swiping left or right, creating a more interactive and mobile-friendly experience. This functionality can significantly improve user engagement...

Disable Slide-In Animation for Divi Gallery Grid Images

Control how images appear in your Divi Gallery module by toggling the slide-in animation effect for grid layouts. Disabling the slide-in animation allows gallery images to load instantly and appear statically, providing a faster and distraction-free browsing...

Control Image Count Display in Divi Gallery Lightbox

Displaying or hiding the image count in the Divi Gallery module’s lightbox can help customize the user experience, depending on whether you want to give visitors an indication of gallery progress or prefer a cleaner, distraction-free view. The ability to toggle this...

Hide Gallery Image Titles in the Divi Lightbox Overlay

Displaying image titles in the lightbox overlay of the Divi Gallery module can sometimes be distracting or unnecessary, depending on your website’s design and user experience goals. Hiding these titles creates a cleaner and more focused viewing experience for visitors...

Random Posts