简体   繁体   中英

Elementor Custom Widget Color Control does not work in live editing

I am working on a custom widget and I want to add styling configuration to the widget. One small problem that I found is that the color control in live editing does not work (It will work for preview and after save then reload).

As shown below, Tab's title and content font color does not change in live editor. elementor实时编辑器

When you click on preview, it works as expected. 预览模式下的元素

I started with the sample code provided on the Elementor Developers page :

echo '<h2 class="title" style="color: ' . $settings['title_color'] . '"> .. </h2>';

However I am getting the same results as describe in the first section.

I then tried to pass parameters to css class, as suggested in this stack overflow post . Though it was successfully set, but it was not set correctly.
My php file:

echo "<p id='narrative' style=' --fontColor : ".$settings['content_color']."'></p>";

In my css file:

#narrative {color: var(--fontColor);}

在此处输入图片说明
When I check on dev tool:
在此处输入图片说明

I am not really sure what went wrong, greatly appreciate if anyone could point me to the right direction. Thank you.

After reading through the sample source code on Elementor Code Reference .

I found out that I missed out a two important steps:

  1. add selector to your class attribute when adding new color control:
$this->add_control(
    'border_color',
    [
        'label' => __( 'Border Color', 'elementor' ),
        'type' => \Elementor\Controls_Manager::COLOR,
        'selectors' => [
            '{{WRAPPER}} .title' => 'color: {{VALUE}};', <-- add selector
        ],
    ]
);
  1. (Important) Add _content_template() function, this function is to generate live preview: Just to note that any for loops in this function should be written in javascript with <# #> instead of php in render().
<h2 class="title"> .. </h2>';

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM