D
D
Dmitry Klimantovich2020-06-09 16:14:15
WordPress
Dmitry Klimantovich, 2020-06-09 16:14:15

How to remove the serial number in assigning an ID to a widget?

making my own widget

class bg_widget extends WP_Widget {

  // Регистрация виджета, используя основной класс
  public function __construct() {
    // вызов конструктора выглядит так:
    // __construct( $id_base, $name, $widget_options = array(), $control_options = array() )
    parent::__construct( 
      'BookForm',
      'BookForm_Widget',
      array( // $widget_options
        'classname' => 'bookform__block',
        'description' => 'Расчет стоимости из даты и количества гостей ',
      )
    );
  }

I set $id_base - BookForm, and it is displayed on the site
<section id="bookform-4" class="widget bookform__block"></section>

Moreover, with each registration of the same name, it, upon withdrawal, assigns a new number.

How can I remove this serial number and save the case so that I can address my widget with a script and not be afraid that it will then assign a different number and the script will not find the widget?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Denis Yanchevsky, 2020-06-09
@deniscopro

According to the standard, the id must be unique on the page, and WordPress generates it based on the widget id + widget instance number, which is logical, because the widget itself is one, and there can be many instances.
If you need a custom id and/or class for each instance, then try the Widget CSS Classes or Widget Options plugin .

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question