【问题标题】:Remove Parent Array at source (Saving Options to Wordpress)从源中删除父数组(将选项保存到 Wordpress)
【发布时间】:2014-05-14 10:20:52
【问题描述】:

编辑:底部添加了更新。

我正在提交一个要保存为 Wordpress 选项的数组。我用print_r/var_dump做了一些调试,可以看到提交的数组被Array ( [string] =>包裹了。

下面我提供了保存的数组和提交的数组之间的比较。

我相信这就是数组没有正确保存的原因。该数组由 Wordpress 简单设置代码使用 $_REQUEST

获取

理想情况下,我想在没有这个包装器的情况下提交这个。或者我可以修改simple-settings class

意图摘要

允许用户添加和删除单个项目(使用 jquery)到保存在 Wordpress 选项中的数组(使用 wordpress-simple-settings)。

资源

保存的数组

Array ( 
    [0] => woocommerce/woocommerce.php 
    [1] => wordpress-seo/wp-seo.php 
    [2] => usersnap/usersnap.php 
    [3] => video-user-manuals/plugin.php 
    [4] => woodojo/woodojo.php 
    [5] => worker/functions.php 
    [6] => advanced-custom-fields/acf.php 
    [7] => admin-menu-editor-pro/menu-editor.php 
    [8] => image-widget/image-widget.php 
    [9] => bwp-minify/bwp-minify.php 
    [10] => cookies-for-comments/cookies-for-comments.php 
    [11] => debug-bar/debug-bar.php 
    [12] => woocommerce-debug-bar/woocommerce-debug-bar.php 
    [13] => woothemes-updater 
)

正在发送待保存的数组

Array ( 
    [string] => Array ( 
        [0] => woocommerce/woocommerce.php 
        [1] => wordpress-seo/wp-seo.php 
        [2] => usersnap/usersnap.php 
        [3] => video-user-manuals/plugin.php 
        [4] => woodojo/woodojo.php 
        [5] => worker/functions.php 
        [6] => advanced-custom-fields/acf.php 
        [7] => admin-menu-editor-pro/menu-editor.php 
        [8] => image-widget/image-widget.php 
        [9] => bwp-minify/bwp-minify.php 
        [10] => cookies-for-comments/cookies-for-comments.php 
        [11] => debug-bar/debug-bar.php 
        [12] => woocommerce-debug-bar/woocommerce-debug-bar.php 
        [13] => woothemes-updater 
) ) 

从输入表单

<input type="text" 
    name="<?php echo $Plugin->get_field_name('req_plugins_arr'); ?>[]" 
    value="<?php echo $value; ?>" 
    id="box<?php echo $key+1; ?>" />

在浏览器中输出的输入表单

<form method="post" action="/wp-admin/admin.php?page=bamboo-settings">
        <input type="hidden" id="bamboo_save" name="bamboo_save" value="c8ef4deceb"><input type="hidden" name="_wp_http_referer" value="/wp-admin/admin.php?page=bamboo-settings">

        <table class="form-table">
            <tbody>
                <tr class="my-form">
                    <th scope="row" valign="top">Required Plugins List</th>

                    <td>
                        <p class="text-box"><label for="box1">Plugin <span class="box-number">1</span> <input type="text" name="bamboo_setting[req_plugins_arr][string][]" value="woocommerce/woocommerce.php" id="box1"></label> <a href="#" class="add-box">Add More</a></p>

                        <p class="text-box"><label for="box2">Plugin <span class="box-number">2</span> <input type="text" name="bamboo_setting[req_plugins_arr][string][]" value=" wordpress-seo/wp-seo.php" id="box2"></label> <a href="#" class="remove-box">Remove</a></p>

                        <p class="text-box"><label for="box3">Plugin <span class="box-number">3</span> <input type="text" name="bamboo_setting[req_plugins_arr][string][]" value=" usersnap/usersnap.php" id="box3"></label> <a href="#" class="remove-box">Remove</a></p>

                        <p class="text-box"><label for="box4">Plugin <span class="box-number">4</span> <input type="text" name="bamboo_setting[req_plugins_arr][string][]" value=" video-user-manuals/plugin.php" id="box4"></label> <a href="#" class="remove-box">Remove</a></p>

                        <p class="text-box"><label for="box5">Plugin <span class="box-number">5</span> <input type="text" name="bamboo_setting[req_plugins_arr][string][]" value="
woodojo/woodojo.php" id="box5"></label> <a href="#" class="remove-box">Remove</a></p>

                        <p class="text-box"><label for="box6">Plugin <span class="box-number">6</span> <input type="text" name="bamboo_setting[req_plugins_arr][string][]" value="
worker/functions.php" id="box6"></label> <a href="#" class="remove-box">Remove</a></p>

                        <p class="text-box"><label for="box7">Plugin <span class="box-number">7</span> <input type="text" name="bamboo_setting[req_plugins_arr][string][]" value="
advanced-custom-fields/acf.php" id="box7"></label> <a href="#" class="remove-box">Remove</a></p>

                        <p class="text-box"><label for="box8">Plugin <span class="box-number">8</span> <input type="text" name="bamboo_setting[req_plugins_arr][string][]" value="
admin-menu-editor-pro/menu-editor.php" id="box8"></label> <a href="#" class="remove-box">Remove</a></p>

                        <p class="text-box"><label for="box9">Plugin <span class="box-number">9</span> <input type="text" name="bamboo_setting[req_plugins_arr][string][]" value="
image-widget/image-widget.php " id="box9"></label> <a href="#" class="remove-box">Remove</a></p>

                        <p class="text-box"><label for="box10">Plugin <span class="box-number">10</span> <input type="text" name="bamboo_setting[req_plugins_arr][string][]" value="
bwp-minify/bwp-minify.php" id="box10"></label> <a href="#" class="remove-box">Remove</a></p>

                        <p class="text-box"><label for="box11">Plugin <span class="box-number">11</span> <input type="text" name="bamboo_setting[req_plugins_arr][string][]" value="
cookies-for-comments/cookies-for-comments.php" id="box11"></label> <a href="#" class="remove-box">Remove</a></p>

                        <p class="text-box"><label for="box12">Plugin <span class="box-number">12</span> <input type="text" name="bamboo_setting[req_plugins_arr][string][]" value="
debug-bar/debug-bar.php" id="box12"></label> <a href="#" class="remove-box">Remove</a></p>

                        <p class="text-box"><label for="box13">Plugin <span class="box-number">13</span> <input type="text" name="bamboo_setting[req_plugins_arr][string][]" value="
woocommerce-debug-bar/woocommerce-debug-bar.php" id="box13"></label> <a href="#" class="remove-box">Remove</a></p>

                        <p class="text-box"><label for="box14">Plugin <span class="box-number">14</span> <input type="text" name="bamboo_setting[req_plugins_arr][string][]" value="
woothemes-updater" id="box14"></label> <a href="#" class="remove-box">Remove</a></p>
                    </td>
                </tr>
            </tbody>
        </table><input class="button-primary" type="submit" value="Save Settings">
    </form>

更新

有点进步。如果我将输入修改为:

PHP:

        <input type="text" name="<?php echo $BambooPlugin->get_field_name('req_plugins_arr', $type = "array"); ?>" value="" id="box1" />

HTML 输出:

    <input type="text" name="bamboo_setting[req_plugins_arr][]" value="woothemes-updater" id="box14">

如果我在保存函数之前 print_r,我会为每个条目打印出这个数组。

例如。

Array ( [0] => a [1] => b [2] => c [3] => d ) 
Array ( [0] => a [1] => b [2] => c [3] => d ) 
Array ( [0] => a [1] => b [2] => c [3] => d ) 
Array ( [0] => a [1] => b [2] => c [3] => d )

然后只保存数组中的最后一项为

    Array ( [0] => d )

我认为这可能需要修改保存设置(行:129)[https://github.com/clifgriffin/wordpress-simple-settings/blob/master/wordpress-simple-settings.php#L129]

【问题讨论】:

  • 您能否编辑您的问题并添加浏览器中显示的输入表单代码。
  • 已经完成了。感谢收看。
  • 刚刚发现名称中的 [req_plugins_arr][string][]... 我知道这是怎么回事。不知道如何从那里删除。
  • 也就是简单设置的default behavior。尝试用get_field_name('req_plugins_arr', 'array');替换get_field_name('req_plugins_arr');
  • 嗨 A.Jesin - 取得了一点进展,并在更新中列出了上面的内容。

标签: php jquery arrays wordpress


【解决方案1】:

好的,所以这实际上是有效的,我的框架只有几个令人困惑的方面,我将不得不澄清一下。基本上,我尝试提供字符串和数组等数据类型是错误的,只会引起混乱。这实际上只是为了管理以分号分隔的字符串,但我设法让它变得混乱和分散注意力。

基本上,存储真正的值数组的正确方法是使用默认的get_field_name,然后添加括号以指示它是一个数组,就像使用直接 HTML 一样.

例子:

<input type="text" name="<?php echo $BambooPlugin->get_field_name('req_plugins_arr'); ?>[]" value="<?php echo $value; ?>" id="box<?php echo $key+1; ?>" />

该框架很好地解决了您的问题。即使生成的字段名称为bamboo_setting[req_plugins_arr][string][],如果您查看“字符串”的实际处理,它也是一个传递。原始值无需转换即可存储。这是我的设计缺陷的核心……我应该接受传递,就像底层的 WordPress 选项 API 一样,而不是试图描述类型。

我可能会更改所有“数组”内容并将其改为分隔符处理程序,然后让其他所有内容直接通过。

因此,相反,当您将数据拉回时,您只需使用:

$default_plugins = $BambooPlugin->get_setting('req_plugins_arr');

这会检索未转换的原始数据。如果您在这种情况下将数组传递给 get_setting ,它会将所有内容都搞砸,因为它会尝试将其视为分隔字符串。 :-/

我希望这会有所帮助...我意识到您在这里花了几天时间与我的框架作斗争,所以我深表歉意。这与我创建它的意图完全相反。我想节省大家的时间:)

【讨论】:

  • 没有浪费时间。它是最好的学习方式。非常感谢您的帮助。期待更新的框架,然后在这里发布更新。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-03-03
  • 1970-01-01
  • 1970-01-01
  • 2021-10-03
  • 1970-01-01
  • 1970-01-01
  • 2016-02-12
相关资源
最近更新 更多