Last active
June 14, 2021 21:40
-
-
Save rtpHarry/2178438a25f5f07a7c258ccb865ba723 to your computer and use it in GitHub Desktop.
Inject a randomised prefix into Elementor Image Carousels so that it separates out the lightboxes contained in JetEngine listings
This file contains 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 | |
/* | |
Plugin Name: SOKA Elementor Image Carousel Gallery ID | |
Plugin URI: https://sokastudio.co.uk/ | |
Text Domain: soka-elementor-gallery-id | |
Description: Control the gallery id to separate out lightbox galleries in JetEngine listings | |
Version: 1.2.0 | |
Author: Matthew Harris, SOKA Studio | |
Author URI: https://sokastudio.co.uk/ | |
License: GPLv2 or later | |
License URI: http://www.gnu.org/licenses/gpl-2.0.html | |
Copyright 2021 Matthew Harris | |
this program is free software; you can redistribute it and/or modify | |
it under the terms of the GNU General Public License, version 2, as | |
published by the Free Software Foundation. | |
This program is distributed in the hope that it will be useful, | |
but WITHOUT ANY WARRANTY; without even the implied warranty of | |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
GNU General Public License for more details. | |
You should have received a copy of the GNU General Public License | |
along with this program; if not, write to the Free Software | |
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA | |
*/ | |
if ( ! defined( 'WPINC' ) ) { | |
die; | |
} | |
/** | |
* Customise the output html of image-carousels | |
* | |
* @since 1.0.0 | |
* @see https://code.elementor.com/php-hooks/ | |
*/ | |
add_action( 'elementor/widget/render_content', function( $content, $widget ) { | |
if ( 'image-carousel' === $widget->get_name() ) { | |
$settings = $widget->get_settings(); | |
if ( ! empty( $settings['random_lightbox_gallery_id'] ) ) { | |
$content = soka_modify_gallery_id($content); | |
} | |
} | |
return $content; | |
}, 10, 2 ); | |
/** | |
* Inject a random gallery id prefix | |
* | |
* @since 1.0.0 | |
*/ | |
function soka_modify_gallery_id($content) { | |
$key = 'data-elementor-lightbox-slideshow="'; | |
$random_id = apply_filters('soka/elementor/image_carousel/gallery_id/random_id', rand(1000, 9999) . '-' . rand(1000,9999)); | |
$key_prefixed = $key . $random_id . '-'; | |
$content = str_replace($key, $key_prefixed, $content); | |
return $content; | |
} | |
/** | |
* Add a control to toggle it on and off | |
* | |
* @since 1.1.0 | |
*/ | |
add_action( 'elementor/element/image-carousel/section_image_carousel/before_section_end', function( $element, $args ) { | |
/** @var \Elementor\Element_Base $element */ | |
$element->add_control( | |
'random_lightbox_gallery_id', | |
[ | |
'label' => __( 'Random Lightbox Gallery ID', 'soka-elementor-gallery-id' ), | |
'description' => __('Prefix lightbox with a random gallery id', 'soka-elementor-gallery-id'), | |
'type' => \Elementor\Controls_Manager::SWITCHER, | |
'label_on' => __( 'Yes', 'soka-elementor-gallery-id' ), | |
'label_off' => __( 'No', 'soka-elementor-gallery-id' ), | |
'return_value' => 'yes', | |
'default' => '', | |
'condition' => [ | |
'open_lightbox!' => 'no', | |
], | |
] | |
); | |
}, 10, 2 ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment