Skip to content

widget hooks work differently after last update #6189

@fhwedos

Description

@fhwedos

We use (among other) Elementor widget Counter. But we need to fill shortcode as Ending number in Counter widget settings instead of static number value. We use Elementor hooks to add new control to Counter widget settings which allows to select if the Ending Number value is static value or shortcode.

We have made our WP plugin which replaces shortcodes with data obtained from our API. In this plugin we have defined hooks for modifying Counter widget so it is able to work with shortcodes. Bellow I attach current code of these hooks from our plugin. This solution worked in version 2.2.7 of Elemetor but after update to the newest version of Elementor it is no more working.

What is now the proper way to modify the Counter widget according to our needs? We have similar problem with Price Table widget of Elementor.

Thank you in advance

The code from our plugin to modify Counter widget:

Hooks:

$this->loader->add_action('elementor/element/counter/section_counter/after_section_start', $this, 'elementor_counter_widget_add_new_controls', 10, 2); 
$this->loader->add_action('elementor/element/counter/section_counter/after_section_end', $this, 'elementor_counter_widget_update_ending_number_control', 10, 2);
$this->loader->add_action('elementor/widget/before_render_content', $this, 'elementor_widget_before_render_content', 10, 1);

Callback functions:

public function elementor_counter_widget_update_ending_number_control($element, $args)  {
  $element->update_control(
    'ending_number',
    [
      'type'        => \Elementor\Controls_Manager::TEXTAREA,
      'label'       => 'Konečné číslo',
      'default'     => 0,
    ]
  );
}

public function elementor_counter_widget_add_new_controls($element, $args)
{
 $element->add_control(
   'ending_number_type',
   [
     'type'        => \Elementor\Controls_Manager::SELECT,
     'label'       => 'Způsob zadání konečného čísla',
     'label_block' => true,
     'default'     => 'constant',
     'options'     => [
       'constant'    => 'konstantní hodnota',
       'shortcode'   => 'shortcode',
     ]
   ]
 );

 $element->add_control(
   'ending_number_constant',
   [
     'type'        => \Elementor\Controls_manager::NUMBER,
     'label'       => 'Konečné číslo',
     'default'     => 0,
     'condition'   => [
       'ending_number_type'  => 'constant',
     ],
   ]
 );

 $element->add_control(
   'ending_number_shortcode',
   [
     'type'        => \Elementor\Controls_manager::TEXTAREA,
     'label'       => 'Konečné číslo - shortcode',
     'condition'   => [
       'ending_number_type'  => 'shortcode',
     ],
   ]
 );

 $element->add_control(
   'ending_number_default',
   [
     'type'        => \Elementor\Controls_manager::NUMBER,
     'label'       => 'Konečné číslo (výchozí)',
     'condition'   => [
       'ending_number_type'  => 'shortcode',
     ],
     'description' => 'použije se v případě, že se nepodaří zízkat hodnoty nahrazením zkratky',
   ]
 );
}

public function elementor_widget_before_render_content($widget)
{
 switch ($widget->get_name())
 {
   case 'counter';
     $this->elementor_widget_counter_render($widget);
     break;

   case 'price-table':
     $this->elementor_widget_price_table_render($widget);
     break;
 }
}

private function elementor_widget_counter_render($counter_widget)
{
  $settings = $counter_widget->get_settings();

  if ($settings['ending_number_type'] === 'shortcode')
  {
     $shortcode = $settings['ending_number_shortcode'];
     $html = do_shortcode($shortcode);

     $count = strip_tags($html);

     if (!is_numeric($count))
       $count = preg_replace("/\D+/", "", $count);

     if (!is_numeric($count))
       $count = $settings['ending_number_default'];
  }
  elseif ($settings['ending_number_type'] === 'constant')
  {
     $count = $settings['ending_number_constant'];
  }

  if (!$count && $settings['ending_number'])
     $count = $settings['ending_number'];

  $counter_widget->set_settings('ending_number', intval($count));
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    status/mergedIndicates when a Pull Request has been merged to a Release.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions