Created
March 19, 2018 20:22
-
-
Save CNDLS/30fcae0bf7416c30678e529b9b9ee4f7 to your computer and use it in GitHub Desktop.
Apply the custom spacing controls to the PHP rendering of the Elementor Section or Column.
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 | |
/* | |
* Utilities - helper functions for Elementor customization. | |
*/ | |
/** | |
* Generate spacing classes for an element given an array of spacing items. | |
* @param $spacing_items Array | |
* @return Array An array where each element is a spacing class. | |
*/ | |
function get_spacing_classes($spacing_items) { | |
$spacing_classes_array = array(); | |
foreach ( $spacing_items as $spacing_item ) { | |
$spacing_classes_array[] = "{$spacing_item['property']}-{$spacing_item['direction']}-{$spacing_item['heading_level']}"; | |
if ($spacing_item['zero_xs'] === 'yes') { | |
$spacing_classes_array[] = "{$spacing_item['property']}-{$spacing_item['direction']}-none-xs"; | |
} | |
if ($spacing_item['zero_sm'] === 'yes') { | |
$spacing_classes_array[] = "{$spacing_item['property']}-{$spacing_item['direction']}-none-sm"; | |
} | |
if ($spacing_item['zero_md'] === 'yes') { | |
$spacing_classes_array[] = "{$spacing_item['property']}-{$spacing_item['direction']}-none-md"; | |
} | |
if ($spacing_item['zero_lg'] === 'yes') { | |
$spacing_classes_array[] = "{$spacing_item['property']}-{$spacing_item['direction']}-none-lg"; | |
} | |
if ($spacing_item['zero_xl'] === 'yes') { | |
$spacing_classes_array[] = "{$spacing_item['property']}-{$spacing_item['direction']}-none-xl"; | |
} | |
} | |
return $spacing_classes_array; | |
} | |
/* | |
* Add custom classes to the Section or Column based on the value of the 'mld_spacing_items' field | |
* that we added above. This hook is called when any element is rendered in the front end | |
* but the user is not using the Elementor Editor or Editor Preview. | |
*/ | |
add_action( 'elementor/frontend/element/before_render', 'before_render_column_or_section'); | |
function before_render_column_or_section($element) { | |
// Check to make sure that the element is either a Section or Column | |
$class_name = get_class( $element ); | |
if ( $class_name === 'Elementor\\Element_Column' || $class_name === 'Elementor\\Element_Section') { | |
$settings = $element->get_settings(); | |
/* | |
* Grab spacing data from the $settings array. | |
*/ | |
$spacing_items = get_spacing_classes($settings['mld_spacing_items']); | |
if ( $settings['spacing_items_override'] !== 'yes' ) { | |
/* | |
* Add the spacing classes to the column or section's wrapper (outermost element). | |
* This is what the '_wrapper' key refers to. The element that the spacing class is | |
* added to will be different than the element that the spacing class is added to in | |
* the Editor Preview, due to limitations in the plugin for how the Section and Column elements | |
* are set up in the Editor Preview versus on the front end. | |
* add_render_attribute() is defined in Element_Base class. | |
*/ | |
$element->add_render_attribute('_wrapper', 'class', $spacing_items); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment