skip to Main Content

I’m using Elementor Pro and want to display an image via a custom shortcode (https://docs.elementor.com/article/449-dynamic-shortcode):

But it isn’t working. There is simply nothing displayed. Neither in the editor, nor in the frontend. Also not if I remove the brackets [ … ] around the shortcode and try brand_banner only.

It does not produce any DOM output in the frontend, just an empty elementor-widget-wrap div:

<div class="elementor-element elementor-element-4f1547ad elementor-column elementor-col-50 elementor-inner-column" data-id="4f1547ad" data-element_type="column">
    <div class="elementor-column-wrap  elementor-element-populated">
        <div class="elementor-widget-wrap">
        </div>
    </div>
</div>

However, if I use a simple text editor widget and put the shortcode in, it works as expected:

DOM:

<div class="elementor-element elementor-element-37728dd elementor-widget elementor-widget-text-editor" data-id="37728dd" data-element_type="widget" data-widget_type="text-editor.default">
    <div class="elementor-widget-container">
        <div class="elementor-text-editor elementor-clearfix">
            <img src="https://some.domain.com/wp-content/uploads/banner-a.jpg"></div>
        </div>
    </div>
</div>

This is the code for the shortcode:

public function shortcode_brand_banner( $atts )
{
    $brand = $this->getRandomBrandFromPool();
    $variantIndex = 1;
    $variantLabel = [ '0', 'a', 'b', 'c' ];
    // Return the rendered image HTML.
    return ( types_render_field( 'image-brand-' . $variantLabel[ $variantIndex ], [ 'post_id' => $brand->post->ID ] ) );
}

I thought maybe it is because it doesn’t expect rendered image HTML, but only an image URL. So I changed the code to this, but it doesn’t work either:

public function shortcode_brand_banner( $atts )
{
    $brand = $this->getRandomBrandFromPool();
    $variantIndex = 1;
    $variantLabel = [ '0', 'a', 'b', 'c' ];
    // Only return image URL like "https://some.domain.com/wp-content/uploads/banner-a.jpg"
    $image = get_post_meta( $brand->post->ID, 'wpcf-image-brand-' . $variantLabel[ $variantIndex ] )[ 0 ];
    return ( $image );
}

What am I doing wrong here? How can I display an image via Dynamic Shortcode?

2

Answers


  1. Chosen as BEST ANSWER

    To close this question, long story short: The image widget was kind of never supposed to support the "Shortcode" feature, because internally the function expects an array, which a Shortcode can not return.

    The Shortcode feature was now removed from the Image widget.

    See: https://github.com/elementor/elementor/issues/7730


  2. EDIT 2020 : ShortCode image URL with Elementor PRO

    For people that want to use a Dynamic Image URI from Shortcode.

    There is the code i’ve done for it
    (you have to put it in a plugin)

    use ElementorControls_Manager;
    add_action( 'elementor/dynamic_tags/register_tags', function( $dynamic_tags ) {
            class Custom_Image_Tag extends ElementorCoreDynamicTagsData_Tag {
    
                public function get_name() {
                    return 'shortcode-image';
                }
    
                public function get_categories() {
                    return [ 'image' ];
                }
    
                public function get_group() {
                    return [ 'site' ];
                }
    
                public function get_title() {
                    return 'Shortcode Image';
                }
    
                protected function _register_controls() {
                    $this->add_control(
                        'shortcode',
                        [
                            'label' => __( 'Shortcode', 'elementor-pro' ),
                            'type'  => Controls_Manager::TEXTAREA,
                        ]
                    );
                }
    
                protected function get_value( array $options = [] ) {
                    $settings = $this->get_settings();
    
                    if ( empty( $settings['shortcode'] ) ) {
                        return;
                    }
                    
                    return [
                        'url' => do_shortcode( $settings['shortcode'] ) // Your shortcode MUST return a full valid URL
                    ];
                }
            }
            $dynamic_tags->register_tag( 'Custom_Image_Tag' );
    });
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search