PHP的 - Wordpress的 - 插件小部件更新功能 - 更新数组值[Foreach循环不工作] [英] PHP - Wordpress - Plugin Widget Update Function - Update array values [Foreach Loop not Working]

查看:150
本文介绍了PHP的 - Wordpress的 - 插件小部件更新功能 - 更新数组值[Foreach循环不工作]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个带有小部件的wordpress插件。目前,小部件的更新功能如下所示。

 函数更新($ new,$ old){
$实例= $旧;
//更新值
$ instance ['element-one'] = $ new ['element-one'];
$ instance ['element-two'] = $ new ['element-two'];
$ instance ['element-three'] = $ new ['element-three'];
$ instance ['element-four'] = $ new ['element-four'];
//返回新实例
return $ instance;

并且这样工作。但是我有一个很长的元素列表,为了清晰的代码,我试图用一个简单的函数来实现它们,如下所示:

pre $ code> function update($ new,$ old){
$ instance = $ old;
//更新值
foreach($ instance = $ k => $ v){
$ instance [$ k] = $ new [$ k];
}
//返回新的实例
return $ instance;

虽然这似乎不起作用。如果我使用这个函数,Widget值不会被更新。所以只是为了测试它是否以我想要的方式工作...我写了一个示例脚本,它工作正常。

  $ a =数组(
'a'=>'1',
'b'=>'2',
'c'=>'3'
);

=数组(
'a'=>'A',
'b'=>'B',
'c'=> ;'C'
);

函数swap_values($ old,$ new){
$ result = $ old;
foreach($ result为$ k => $ v){
$ result [$ k] = $ new [$ k];
}
return $ result;
}

$ res = swap_values($ a,$ b);
var_dump($ res);

这个脚本工作正常,它交换了数组的值,但似乎由于某些原因,这是



另外一些奇怪的事情是我在工作时意识到的:

$ ul $ b

  • 如果假设我有 $实例数组在窗体函数中声明了几个元素小部件和更新功能正在更新它们像 $实例['旧'] = $实例['新']; 它工作正常。另外如果我有插件安装和激活,然后我更改更新函数使用 foreach循环他们工作正常。

  • 但然后,我已经改变了更新函数使用foreach循环,如果我再添加元素到 $实例数组它们不被更新,而以前声明的元素是。 >
  • 另外,如果我安装并激活这个插件[在更新更新函数使用foreach循环后]在一个单独的wordpress安装,没有任何元素似乎更新。
  • ul>

    我相信有一些我很想念的东西。任何帮助或建议,你将不胜感激。
    解决方案

    我找到了一个简单的解决方案:不包括一个更新函数核心只返回$ new_instance变量作为默认值(wp-includes / widgets.php):

     函数更新($ new_instance,$ old_instance){
    return $ new_instance;
    }

    我已经在自己的小部件中成功测试了这一点,自己的更新功能对过滤用户输入非常有用,但似乎没有必要。


    i am in the process of developing a wordpress plugin with a widget. Currently the update function for the widget looks like this.

    function update($new, $old){
        $instance = $old;
        //Update Values
        $instance['element-one'] = $new['element-one'];
        $instance['element-two'] = $new['element-two'];
        $instance['element-three'] = $new['element-three'];
        $instance['element-four'] = $new['element-four'];
        //Return New Instance
        return $instance;
    

    and this works as it is supposed to. But i have a long list of elements and for the sake of cleanliness of code i am trying to achieve them with a simple function as follows:

    function update($new, $old){
        $instance = $old;
        //Update Values
        foreach($instance as $k => $v){
            $instance[$k] = $new[$k];
        }
        //Return New Instance
        return $instance;
    

    Though this doesn't seem to work. The Widget values are not being updated if i use this function. So just to test if it works the way i want it to... i wrote a sample script which is working fine. The script is as follows.

    $a = array(
      'a' => '1',
      'b' => '2',
      'c' => '3'
    );
    
    $b = array(
      'a' => 'A',
      'b' => 'B',
      'c' => 'C'
    );
    
    function swap_values($old, $new){
      $result = $old;
      foreach($result as $k => $v){
        $result[$k] = $new[$k];
      }
      return $result;
    }
    
    $res = swap_values($a, $b);
    var_dump($res);
    

    This script is working fine and it swaps the values of the arrays but it seems for some reason this is not doing well in wordpress.

    Another Few strange things that i realized while working on this is

    • if suppose i have a few elements declared in the $instance array in the form function of the widget and the update function is updating them like $instance['old'] = $instance['new']; it works fine. Also if i have the plugin installed and activated and then i change the update function to use the foreach loop they work fine.
    • But Then after i have changed the update function to use foreach loop if i add anymore elements to the $instance array they are not being updated, whereas previously declared elements are.
    • Also if i install and activate this plugin [after updating the update function to use foreach loop] on a separate wordpress installation none of the elements seem to update.

    I am sure there is something very minor that i am missing. Any help or suggestions from you would be greatly appreciated.

    解决方案

    I have found a simple solution: do not include an update function in your extension of the widget.

    The core simply returns the $new_instance variable as a default (wp-includes/widgets.php):

    function update($new_instance, $old_instance) {
        return $new_instance;
    }
    

    I have tested this with success in my own widget and have determined that making your own update function is useful for filtering user input, but it does not seem necessary.

    这篇关于PHP的 - Wordpress的 - 插件小部件更新功能 - 更新数组值[Foreach循环不工作]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆