Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save xlplugins/e66b289ae4552c886c7806515c14b67c to your computer and use it in GitHub Desktop.
Save xlplugins/e66b289ae4552c886c7806515c14b67c to your computer and use it in GitHub Desktop.
Dequeue elementor scripts on page to avoid error when page is not built with elementor
/**
* Dequeues Elementor scripts and styles for FunnelKit Builder pages
*
* This function prevents loading unnecessary Elementor assets on FunnelKit Builder pages
* that are using canvas or boxed templates but not built with Elementor. This helps
* improve page load performance.
*
* The function checks:
* 1. If Elementor locations exist
* 2. If current post exists and is valid
* 3. If post type matches FunnelKit Builder types
* 4. If page template is canvas or boxed
* 5. If page is NOT built with Elementor
*
* @return void
* @uses \ElementorPro\Plugin::instance()
* @uses get_post_meta()
* @uses wp_dequeue_script()
* @uses wp_dequeue_style()
*
* @global WP_Post $post Current post object
*
* @since 3.10.2
*/
function fb_dequeue_scripts_if_page_is_not_built_using_elem() {
global $post;
try {
if ( ! class_exists( '\ElementorPro\Plugin', false ) ) {
return;
}
// Get Elementor theme builder locations
$locations = \ElementorPro\Plugin::instance()->modules_manager->get_modules( 'theme-builder' )->get_locations_manager()->get_locations();
if ( empty( $locations ) ) {
return;
}
//Check if we either global header or footer set using theme builder
if ( ( array_key_exists( 'header', $locations ) || array_key_exists( 'footer', $locations ) ) ) {
// Validate post object
if ( is_null( $post ) || ! $post instanceof WP_Post ) {
return;
}
// Check if post type is a FunnelKit Builder type
if ( ! in_array( $post->post_type, array(
'wffn_landing', // Landing pages
'wffn_ty', // Thank you pages
'wffn_optin', // Optin pages
'wffn_oty', // Optin thank you pages
'wfacp_checkout', // Checkout pages
'wfocu_offer', // One click upsell offers
), true ) ) {
return;
}
// Check if using canvas or boxed template
$page_template = get_post_meta( $post->ID, '_wp_page_template', true );
if ( false !== strpos( $page_template, '-canvas.php' ) || false !== strpos( $page_template, '-boxed.php' ) ) {
// Return if page is built with Elementor
if ( \Elementor\Plugin::$instance->documents->get( $post->ID )->is_built_with_elementor() ) {
return;
}
// Dequeue Elementor scripts and styles since page is not built with Elementor
wp_dequeue_script( 'elementor-frontend' );
wp_dequeue_style( 'elementor-frontend' );
wp_dequeue_script( 'elementor-pro-frontend' );
wp_dequeue_style( 'elementor-pro-frontend' );
wp_dequeue_script( 'elementor-sticky' );
wp_dequeue_script( 'elementor-waypoints' );
wp_dequeue_script( 'elementor-frontend-modules' );
}
}
} catch ( Exception|Error $e ) {
// Silently fail if any errors occur
}
}
add_action( 'wp_enqueue_scripts', 'fb_dequeue_scripts_if_page_is_not_built_using_elem', 12 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment