我有一个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 );
}
在widget方法中调用change_numeric_indexes_to_names()方法。由于它没有在表单方法中运行,我试图绕过必须进入每个网站实例,打开每个小部件并保存。相反,我想在检查数字ID后切换存储的数据库选项值。
正如您在update_widgets_ids_with_names()方法中看到的那样,我尝试挂钩到更新方法。但是进入wp-includes / default-widgets.php我看到该选项仅更新对象但没有保存值。
您能否建议一种保存值的方法,以便在加载网站时更新窗口小部件的每个实例?
此致 史蒂夫
答案 0 :(得分:0)
所以我明白了。
在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 );
}
此致 史蒂夫