Skip to content

Instantly share code, notes, and snippets.

@mwender
Created June 12, 2024 14:27
Show Gist options
  • Save mwender/49d4f063f11a1b29ebac7f91c43e31c6 to your computer and use it in GitHub Desktop.
Save mwender/49d4f063f11a1b29ebac7f91c43e31c6 to your computer and use it in GitHub Desktop.
[Elementor Omit from Output] Provides a new control to keep Elementor from rendering a container. #elementor #wordpress
<?php
/**
* Taken from [Feature Request: Option to temporarily hide sections](https://github.com/elementor/elementor/issues/18183)
*/
(function() {
# Add new control after the Custom CSS control on the Advanced tab.
add_action(
'elementor/element/after_section_end',
function($element, $section_id, $args) {
if ($section_id === 'section_custom_css') {
$element->start_controls_section(
'omitter_section',
[
'tab' => \Elementor\Controls_Manager::TAB_ADVANCED,
'label' => esc_html__( 'Output', 'yourtextdomain' ),
]
);
$element->add_control(
'omit_element',
[
'type' => \Elementor\Controls_Manager::SWITCHER,
'label' => 'Omit from output',
'label_on' => esc_html__( 'Omit', 'yourtextdomain' ),
'label_off' => esc_html__( 'Inherit', 'yourtextdomain' ),
'description' => esc_html__( 'Prevent element and its associated assets from being rendered in markup, not merely hide it with CSS.', 'yourtextdomain' ),
'selectors_dictionary' => [
'' => '',
'yes' => 'opacity: 0.25; content: " "; display: block; position: absolute; z-index: 99999; width: 100%; height: 100%; background-image: repeating-linear-gradient(45deg, #f6d32d, #f6d32d 5px, #000 7px, #000 17px, #f6d32d 19px, #f6d32d 24px);',
],
'selectors' => [
'{{WRAPPER}}::before' => '{{VALUE}}',
],
]
);
$element->end_controls_section();
}
},
PHP_INT_MAX,
3
);
# Dynamically add filter to each element type, without knowing them in advance.
# There is no generic 'elementor/frontend/should_render' hook, so this acts as an equivalent.
$omitter = function($should_render, $element) {
return $should_render
&& (
!($settings = $element->get_settings())
|| empty($settings['omit_element'])
);
};
$element_types = [];
add_action(
'elementor/frontend/before_render',
function($element) use ($omitter, &$element_types) {
$element_type = $element->get_type();
if (!array_key_exists($element_type, $element_types)) {
add_filter(
"elementor/frontend/$element_type/should_render",
$omitter,
PHP_INT_MAX,
2
);
$element_types[$element_type] = true;
}
},
PHP_INT_MAX
);
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment