Reputation: 61
I am developing my first Wordpress plugin. I've been following some guides on creating a settings page.
I have the following page which correctly displays the value of the fields in the database. When I go to the page, edit the fields and press "Save changes" the changes aren't saved to the database. If I change the values directly in the database, then the values does show up correctly in the input fields, but I still cannot update the values from my page.
Can you see any obvious errors that I've made or things that I'm missing?
<?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
}
?>
Upvotes: 4
Views: 14172
Reputation: 331
It is also worth noting that if you have this problem, check to see if your trying to echo the settings fields.
So for me I had this:
echo '
<div class="wrap">
<h1>Theme Settings <img src="' . get_stylesheet_directory_uri('stylesheet_directory') . '/images/site-icon.png" width="32" height="32" /></h1>
<form method="post" action="options.php">
' . settings_fields( 'custom-settings-group' ) . '
' . do_settings_sections( 'custom-settings-group' ) . '
<table class="form-table">
<tr valign="top">
<th scope="row">Brisbane Hours</th>
<td><textarea rows="4" cols="40" name="brisbane_hours">' . esc_attr( get_option('brisbane_hours') ) . '</textarea></td>
</tr>
<tr valign="top">
<th scope="row">Adelaide Hours</th>
<td><input type="text" value="' . esc_attr( get_option('adelaide_hours') ) . '"/></td>
</tr>
</table>
' . submit_button() . '
</form>
</div>
';
Which means that these two fields were getting echoed:
settings_fields( 'custom-settings-group' )
do_settings_sections( 'custom-settings-group' )
Changing to this fixed it for me.
<?php settings_fields( 'snowys-custom-settings-group' ); ?>
<?php do_settings_sections( 'snowys-custom-settings-group' ); ?>
Upvotes: 2
Reputation: 8070
I checked the code and tested from my side and i did some changes see the complete working code
<?php
/**
* Plugin Name: Testing Plugin
*/
add_action('admin_menu', 'SetupPage');
add_action('admin_init', 'RegisterSettings');
function SetupPage() {
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" action="options.php">
<?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
}
?>
Hope it may help for someone :)
Upvotes: 0
Reputation: 4404
Do you have a multisite set? If so, the setting will be created in wp_options table instread of local wp_*_options single site table. You have to use
add_blog_option( get_current_blog_id(), "option_name", "" );
This ussaly occurs when you have had forced multisite on existing blog and now try to manage options.
Upvotes: 0
Reputation: 376
Which browser are you using? It may sound weird, but using Chrome, I've found a couple of plugins that don't save settings properly.
Not a great technical answer, but if the plugin is just for your own use and you can get its admin functions working in Firefox and IE it may be easier to settle for 'good enough'.
Upvotes: 0
Reputation: 1475
Looks to me like you need to add the action="options.php"
in the form tag. Otherwise it seems right. No doubt you've looked at this codex page, since your code is very similar, but that's about the only difference I see.
Upvotes: 4