【发布时间】:2011-01-31 22:51:05
【问题描述】:
我正在开发我的第一个 Wordpress 插件。我一直在遵循一些有关创建设置页面的指南。
我有以下页面,它正确显示了数据库中字段的值。当我转到该页面时,编辑字段并按“保存更改”,更改不会保存到数据库中。如果我直接在数据库中更改值,那么这些值确实会正确显示在输入字段中,但我仍然无法从我的页面更新值。
您能看出我犯了什么明显的错误或遗漏了什么吗?
<?php
add_action('admin_menu', 'SetupPage');
function SetupPage()
{
add_action('admin_init', 'RegisterSettings');
// Setup administration menu item
if (function_exists('add_options_page'))
{
add_menu_page(__("TestPage"), __("TestPage"), "manage_options", __FILE__, 'PageContent', plugins_url('/images/icon.png', __FILE__));
}
}
function RegisterSettings()
{
// Add options to database if they don't already exist
add_option("test_option1", "", "", "yes");
add_option("test_option2", "", "", "yes");
add_option("test_option3", "", "", "yes");
// Register settings that this form is allowed to update
register_setting('test_settings', 'test_option1');
register_setting('test_settings', 'test_option2');
register_setting('test_settings', 'test_option3');
}
?>
<?php
function PageContent()
{
if (!current_user_can('manage_options'))
wp_die(__("You don't have access to this page"));
?>
<div class="wrap">
<h2><?_e("Test settings")?></h2>
<form method="post">
<?php settings_fields('test_settings'); ?>
<table class="form-table">
<tr valign="top">
<th scope="row">test_option1</th>
<td><input type="text" name="test_option1" value="<?php echo get_option('test_option1'); ?>" /></td>
</tr>
<tr valign="top">
<th scope="row">test_option2</th>
<td><input type="text" name="test_option2" value="<?php echo get_option('test_option2'); ?>" /></td>
</tr>
<tr valign="top">
<th scope="row">test_option3</th>
<td><input type="text" name="test_option3" value="<?php echo get_option('test_option3'); ?>" /></td>
</tr>
</table>
<p class="submit">
<input type="submit" class="button-primary" value="<?php _e('Save changes') ?>" />
</p>
</form>
</div>
<?php
}
?>
【问题讨论】:
-
Otto 的教程会让你走上正轨:ottopress.com/2009/wordpress-settings-api-tutorial
-
我尝试重新开始并按照 Otto 的教程进行操作。现在它正在工作。虽然它没有使用完全相同的方法,但我想我会继续这样做。谢谢
标签: wordpress