Skip to content

Small solution to using in elementor dynamic Tags #147

@anonline

Description

@anonline

Hi!

As mentioned in the readme, it is not support the dynamic tags in elementor know. So I wrote a little class for it. I used these images as background of containers so others can use it or make it better and build it in the app. @joppuyo

function register_cropped_image_dynamic_tag( $dynamic_tags_manager ) {
	$dynamic_tags_manager->register( new \Elementor_Dynamic_Tag_Cropped_Image );
}

add_action( 'elementor/dynamic_tags/register', 'register_cropped_image_dynamic_tag' );

class Elementor_Dynamic_Tag_Cropped_Image extends \Elementor\Core\DynamicTags\Data_Tag {
	public function get_name() {
		return 'cropped-image';
	}

	public function get_title() {
		return esc_html__( 'ACF Cropped image', 'elementor' );
	}

	public function get_group() {
		return [ 'acf' ];
	}

	public function get_categories() {
		return [
			\Elementor\Modules\DynamicTags\Module::GALLERY_CATEGORY,
			\Elementor\Modules\DynamicTags\Module::MEDIA_CATEGORY,
			\Elementor\Modules\DynamicTags\Module::IMAGE_CATEGORY,
			\Elementor\Modules\DynamicTags\Module::URL_CATEGORY
		];
	}
	
	protected function register_controls() {
		global $wpdb;
		
		$fields = array();
		
		$sql = "SELECT post_excerpt as id, post_title as name FROM wp_posts WHERE post_type = 'acf-field' AND post_content LIKE '%image_aspect_ratio_crop%'";
		$results = $wpdb->get_results($sql);
		
		foreach($results as $option){
			$fields[$option->id] = $option->name;
		}
		
		$this->add_control(
			'acf_field',
			[
				'label' => esc_html__( 'Select field', 'elementor' ),
				'type' => \Elementor\Controls_Manager::SELECT,
				'options' => $fields,
			]
		);
		
		$this->add_control(
			'image_size',
			[
				'label' => esc_html__( 'Image size name or size array(\'w\',\'h\')', 'elementor' ),
				'type' => 'text',
				'default' => 'full'
			]
		);
		$this->add_control(
			'fallback_image',
			[
				'label' => esc_html__( 'Fallback image', 'elementor' ),
				'type' => 'media',
			]
		);		
	}

	public function get_value(array $options = []) {
		$field_name = $this->get_settings( 'acf_field' );
		$image_size_input = $this->get_settings( 'image_size' );
		$fallback_image = $this->get_settings( 'fallback_image' );
		
		$image_field = get_post_meta(get_the_ID(), $field_name, true);

		if($image_field === false || $image_field == ""){
			return $fallback_image;
		}
		
		$image_url = "";
		$image_alt = "";
		
		if(is_int($image_field)){
			$image_array = wp_get_attachment_image_src($image_field, $image_size_input);  //if id
			if($image_array !== false && is_array($image_array)){
				$image_url = $image_array[0];
			}
			else{
				return $fallback_image;
			}
		}
		elseif(filter_var($image_field, FILTER_VALIDATE_URL)){ //if url
			$image_url = $image_field;
		}
		elseif(is_array($image_field) && isset($image_field['url'])){  //if array
			$image_url = $image_field['url'];
			$image_alt = isset($image_field['alt']) ? $image_field['alt'] : "";
		}
		
		return array(
			'id' => $image_field,
			'url' => $image_url,
			'alt' => $image_alt,
			"size" => "",
			"source" => "library"
		);
	}
}

image
image
image

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions