我有一个wordpress小部件,它有一个"div ids"的选项列表供用户选择。最初,此选项列表的索引为 0。但是,它需要更改为名称值而不是 0 索引。名称索引现在运行良好。我想看看是否有可能的技巧是使用新的命名值更新站点上的当前实例,将其交换为 0 索引值。我尝试使用以下方法:
private function change_numeric_indexes_to_names( $ids_id, $widget_id ) {
$settings = $this->get_settings();
/*
* If the saved div id is indexed using an integer
* the change it and same it as a name string. This
* is needed as the old div id implementation used numeric indexes.
* To solve the issue if the number / position of the ids changed thus
* rendering the wrong div id. This will need to run only once as subsequent
* calls to the same ad / div id will use a name string instead.
*/
if ( is_numeric( $ids_id ) ) {
$settings['id'] = $ids_id;
$ids_id = [ $ids_id ];
}
$this->update_widgets_ids_with_names( $ids_id ); // Updates the stored widget values with new named id;
return $ids_id;
}
private function update_widgets_ids_with_names( $id ) {
/*
* Get this instances options
*/
$widget_options_all = get_option( $this->option_name );
$old_instance = $widget_options_all[ $this->number ];
$new_instance = $old_instance;
$new_instance['id'] = $id;
return $this->update( $new_instance, $old_instance );
}
change_numeric_indexes_to_names() 方法在小部件方法中调用。由于它没有以表单方法运行,因此我正在尝试绕过进入每个网站实例,打开每个小部件并保存。相反,我想在检查数字 ID 后切换存储的数据库选项值。
正如您在我尝试挂钩到更新方法的 update_widgets_ids_with_names() 方法中看到的那样。但是进入 wp-include/default-widgets.php我看到该选项只是更新对象,但没有发生保存值。
您能否建议一种保存值的方法,以便在加载站点时更新小部件的每个单独实例?
问候史蒂夫
所以我想通了。
在 update_widgets_ids_with_names( $id_now_as_name ) 方法中,需要添加 $this->update_callback();。这是一种危险的方法,因为它真的会把事情搞砸,但就我而言,它似乎工作得很干净,很好。这是有效的更新方法:
private function update_widgets_ids_with_names( $id_now_as_name ) {
/*
* Get this instances options
*/
$widget_options_all = get_option( $this->option_name );
$old_instance = $widget_options_all[ $this->number ];
$new_instance = $old_instance;
$new_instance['id'] = $id_now_as_name;
$this->update_callback(); // this processes the widget settings prior to the update function, it is needed.
return $this->update( $new_instance, $old_instance );
}
问候史蒂夫