Skip to content

Instantly share code, notes, and snippets.

@undfine
Created April 2, 2024 21:50
Show Gist options
  • Save undfine/2b54ab0aa54df3143e1d6813fa150888 to your computer and use it in GitHub Desktop.
Save undfine/2b54ab0aa54df3143e1d6813fa150888 to your computer and use it in GitHub Desktop.
<?php
/**
* prevent Elementor sections and containers from rendering on frontend if they are set to hide in all 3 responsive settings
*/
function do_not_render_hidden_sections( $should_render, $section ) {
// skip check if we are and editing
if ( is_admin() ) { return $should_render; }
$settings = $section->get_settings_for_display();
//$breakpoints = ['desktop', 'tablet', 'mobile', 'laptop', 'tablet_extra', 'widescreen'];
$breakpoints = ['desktop', 'tablet', 'mobile'];
// Check if hidden on all breakpoints
if ( breakpoints_marked_as_hidden($breakpoints, $settings) ){
return false;
}
return $should_render;
}
function breakpoints_marked_as_hidden( $breakpoints, $settings ) {
$hidden = 0;
foreach( $breakpoints as $key ) {
$key = 'hide_'.$key;
if ( isset( $settings[ $key ] ) && !empty( $settings[ $key] )){
$hidden++;
}
}
return ($hidden >= count($breakpoints) );
}
add_filter( 'elementor/frontend/section/should_render', 'do_not_render_hidden_sections', 10, 2 );
add_filter( 'elementor/frontend/container/should_render', 'do_not_render_hidden_sections', 10, 2 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment