【发布时间】:2014-05-14 10:20:52
【问题描述】:
编辑:底部添加了更新。
我正在提交一个要保存为 Wordpress 选项的数组。我用print_r/var_dump做了一些调试,可以看到提交的数组被Array ( [string] =>包裹了。
下面我提供了保存的数组和提交的数组之间的比较。
我相信这就是数组没有正确保存的原因。该数组由 Wordpress 简单设置代码使用 $_REQUEST
理想情况下,我想在没有这个包装器的情况下提交这个。或者我可以修改simple-settings class
意图摘要
允许用户添加和删除单个项目(使用 jquery)到保存在 Wordpress 选项中的数组(使用 wordpress-simple-settings)。
资源
-
http://jesin.tk/dynamic-textbox-jquery-php/
- 这允许添加/删除单个项目然后保存为数组的选项
-
https://github.com/clifgriffin/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