Created
June 30, 2023 17:08
-
-
Save pingram3541/8e04154c24946fb742f948f20105a0a9 to your computer and use it in GitHub Desktop.
Add Render control to all Elementor elements
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Adds Render control to all widgets | |
* | |
* Action: toggle | |
* | |
* (base for adding custom conditions, see Step 3) | |
* | |
**/ | |
// Exit if accessed directly. | |
if ( ! defined( 'ABSPATH' ) ) { | |
exit; | |
} | |
// Step 1: Hook into every widget on the page for it's $stack_name | |
add_action( 'elementor/element/before_section_end', function( $element, $args ) { | |
$stack_name = $element->get_name(); | |
custom_add_elementor_controls_action( $stack_name ); | |
}, 10, 2 ); | |
// Step 2: Add toggle control to every element's ($stack_name) Advanced tab | |
function custom_add_elementor_controls_action( $stack ){ | |
add_action( 'elementor/element/'.$stack.'/_section_style/before_section_end', function( $element, $args ) { | |
/** @var \Elementor\Element_Base $element */ | |
$element->add_control( | |
'disable_render', | |
[ | |
'type' => \Elementor\Controls_Manager::SWITCHER, | |
'label' => __( 'Disable rendering on front end?', 'elementor' ), | |
'label_on' => __( 'On', 'elementor' ), | |
'label_off' => __( 'Off', 'elementor' ), | |
'default' => 'no', | |
] | |
); | |
}, 10, 2 ); | |
} | |
// Step 3. filter to kill render / check conditional | |
add_filter( 'elementor/frontend/widget/should_render', function( $bool, $element ){ | |
$settings = $element->get_settings(); | |
if( ! empty( $settings['disable_render'] ) && 'yes' === $settings['disable_render'] ){ | |
/** | |
* add condition(s) here, if met: | |
* | |
* return true; | |
* | |
* ie if is_user_logged_in() | |
* if $element->get_name() === 'heading' | |
* etc | |
**/ | |
//otherwise | |
return false; //do not render | |
} else { | |
return true; //load widget normally | |
} | |
}, 10, 3); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment