<?php
/**
* Plugin Name: Custom links in Elementor Image Carousel
* Description: Add custom links in Elementor Image Carousel widget
* Version: 1.1.2
* Requires at least: 5.2
* Requires PHP: 7.2
* Author: Charlie Etienne
* Author URI: https://web-nancy.fr
* License: GPL v2 or later
* License URI: https://www.gnu.org/licenses/gpl-2.0.html
* Text Domain: custom-links-eicw
* Domain Path: /languages
*/
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
class CustomLinksEICW {
private static $instance;
final public static function get_instance(): CustomLinksEICW {
if (null === self::$instance) {
self::$instance = new self();
}
return self::$instance;
}
public function init() {
add_filter( 'attachment_fields_to_edit', [ $this, 'attachment_fields_to_edit' ], 10, 2 );
add_filter( 'attachment_fields_to_save', [ $this, 'attachment_fields_to_save' ], 10, 2 );
add_action( 'elementor/frontend/widget/before_render', [ $this, 'modify_carousel_settings' ] );
}
/**
* Adds 'elementor_carousel_custom_link' field to attachment
*
* @param $form_fields
* @param $post
*
* @return array
*/
public function attachment_fields_to_edit( $form_fields, $post ): array {
$form_fields[ 'elementor_carousel_custom_link' ] = array(
'label' => __( 'Custom link', 'elementor' ),
'input' => 'text',
'value' => get_post_meta( $post->ID, 'elementor_carousel_custom_link', true ),
'helps' => __( 'This will add a link to images in Elementor Carousel', 'elementor' ),
);
$target = (bool) get_post_meta( $post->ID, 'elementor_carousel_custom_link_target', true );
$checked = ( $target ) ? 'checked' : '';
$form_fields[ 'elementor_carousel_custom_link_target' ] = array(
'label' => __( 'Open in new tab ?', 'elementor' ),
'input' => 'html',
'html' => "<input type='checkbox' $checked name='attachments[$post->ID][elementor_carousel_custom_link_target]' id='attachments[$post->ID][elementor_carousel_custom_link_target]' />",
'value' => $target,
'helps' => __( 'Open custom link in Elementor Carousel in new tab ?', 'elementor' ),
);
return $form_fields;
}
/**
* Saves 'elementor_carousel_custom_link' field to attachment
*
* @param $post
* @param $attachment
*
* @return WP_Post|array|null
*/
public function attachment_fields_to_save( $post, $attachment ) {
$attachment_link = $attachment[ 'elementor_carousel_custom_link' ];
$formated_link = $attachment_link ?? '';
update_post_meta( $post[ 'ID' ], 'elementor_carousel_custom_link', $formated_link );
$link_target = isset( $attachment[ 'elementor_carousel_custom_link_target' ] ) ? 1 : 0;
update_post_meta( $post[ 'ID' ], 'elementor_carousel_custom_link_target', $link_target );
return $post;
}
/**
* Modify Carousel settings
*/
public function modify_carousel_settings( $widget ) {
if ( 'image-carousel' !== $widget->get_name() ) {
return;
}
$settings = $widget->get_settings_for_display();
$slides = $settings['carousel_images'];
if ( ! $slides || empty( $slides ) ) {
return;
}
$slides_with_links = [];
foreach ( $slides as $index => $attachment ) {
$link_url = get_post_meta( $attachment['id'], 'elementor_carousel_custom_link', true );
$link_target = (bool) get_post_meta( $attachment['id'], 'elementor_carousel_custom_link_target', true );
$attachment['url'] = ! empty( $link_url ) ? $link_url : '';
$attachment['is_external'] = $link_target;
$slides_with_links[ $index ] = $attachment;
}
$widget->set_settings( 'carousel_images', $slides_with_links );
}
}
CustomLinksEICW::get_instance()->init();
Im working on a fix. I dont think its right yet.
-
This reply was modified 1 year, 11 months ago by
davidjs.