我下载了一个用于剖析的插件,可以用来学习设置api。这是一个简单的背景上传程序,正如你所看到的那样,非常简单。当尝试使用我自己的选项集重新创建它时,我几乎总是遇到非法偏移警告。在阅读了这个问题之后,问题似乎根源于选项没有被定义为数组,因此无法识别“image”键。我的问题是,在这段代码工作得非常好的情况下,它在脚本中的何处将选项定义为可用数组?我写了看起来完全相同的东西,只是更改了选项名称,并不断收到非法偏移警告。
<script type="text/javascript">
//<![CDATA[
jQuery(document).ready(function()
{
// Media Uploade
window.widgetFormfield = \'\';
jQuery(\'.upload_image_button\').live(\'click\', function() {
window.widgetFormfield = jQuery(\'.upload_field\',jQuery(this).parent());
tb_show(\'Choose Image\', \'media-upload.php?TB_iframe=true\');
return false;
});
window.original_send_to_editor = window.send_to_editor;
window.send_to_editor = function(html) {
if (window.widgetFormfield) {
imgurl = jQuery(\'img\',html).attr(\'src\');
window.widgetFormfield.val(imgurl);
tb_remove();
}
else {
window.original_send_to_editor(html);
}
window.widgetFormfield = \'\';
window.imagefield = false;
jQuery(\'#fsb_preview_image\').attr(\'src\', imgurl);
}
});
//]]>
</script>
<div class="wrap">
<div id="fsb-wrap" class="fsb-help">
<h2>Full Screen Background Image</h2>
<?php
if ( ! isset( $_REQUEST[\'updated\'] ) )
$_REQUEST[\'updated\'] = false;
?>
<?php if ( false !== $_REQUEST[\'updated\'] ) : ?>}
<div class="updated fade"><p><strong><?php _e( \'Options saved\' ); ?></strong></p></div>
<?php endif; ?>
<form method="post" action="options.php">
<?php settings_fields( \'fsb_register_settings_4\' ); ?>
<h4>Choose Your Image</h4>
<p>
<input id="fsb_settings_4[image]" name="fsb_settings_4[image]" type="text" class="upload_field" value="<?php echo $fsb_options[\'image\']; ?>"/>
<input class="upload_image_button button-secondary" type="button" value="Choose Image"/>
<label class="description" for="fsb_settings_2[image]"><?php _e( \'This image will be applied to the background of your website\' ); ?></label>
</p>
<p>
<?php if($fsb_options[\'image\']) { ?>
<img src="<?php echo $fsb_options[\'image\']; ?>" id="fsb_preview_image" style="padding: 3px; border: 1px solid #f0f0f0; max-width: 600px; overflow: hidden;"/>
<?php } else { ?>
<img src="<?php echo plugin_dir_url( __FILE__ ) . \'preview.jpg\'; ?>" id="fsb_preview_image" style="padding: 3px; border: 1px solid #f0f0f0; max-width: 600px; overflow: hidden;"/>
<?php } ?>
</p>
<!-- save the options -->
<p class="submit">
<input type="submit" class="button-primary" value="<?php _e( \'Save Options\' ); ?>" />
<p><?php echo esc_url( $fsb_options[\'image\'] ) ?> </p>
</p>
</form>
</div><!--end fsb-wrap-->
</div><!--end wrap-->
<?php
}
function fsb_init_admin() {
add_menu_page( \'General Settings\', \'General Settings\', \'administrator\', \'display- settings\', \'fsb_admin_page\' );
}
add_action(\'admin_menu\', \'fsb_init_admin\');
// register the plugin settings
function fsb_register_settings() {
register_setting( \'fsb_register_settings_4\', \'fsb_settings_4\' );
}
add_action( \'admin_init\', \'fsb_register_settings\' );
SO网友:tfrommen
数组“隐藏”在表单元素中。他们各自的名称/ID如下MY_option_name[some_other_name]
. 这就是数组结构。
// Edit (对此稍作阐述)
假设您有以下表单:
<form action="options.php" method="post">
Font:<br />
<input type="text" name="MY_options[font]" id="MY_options[font]" />
Color:<br />
<input type="text" name="MY_options[color]" id="MY_options[color]" />
<?php submit_button(); ?>
</form>
如果您提交表单,它将执行以下操作:
$my_options = array(
\'font\' => YOUR_FONT_INPUT,
\'color\' => YOUR_COLOR_INPUT,
);
update_option(\'MY_options\', $my_options);
BUT - 如果您不将其当前值合并到表单中,则之前声明和设置的每个选项都将被取消设置并删除!因此,请务必首先阅读选项并存储您不想更新的选项(但仍想保留!)表单中隐藏的输入字段。像这样:
foreach (get_option(\'MY_options\') as $option_name => $option_value) {
switch ($option_name) {
case \'font\':
case \'color\':
break;
default:
echo \'<input type="hidden" name="\'.$option_name.\'" value="\'.$option_value.\'" />";
}
}