admin管理员组文章数量:1391987
I'm creating a plugin which have few settings, I've created a settings page for it. Here's the code:
function sswc_register_settings()
{ echo "---------------------------------------------register_settings function called!";
add_option('sswc_color_option', '#A18DC6');
add_option('sswc_top_option', '400');
add_option('sswc_left_option', '100');
register_setting('sswc_options_group', 'sswc_color_option');
register_setting('sswc_options_group', 'sswc_top_option');
register_setting('sswc_options_group', 'sswc_left_option');
}
add_action('admin_init', 'sswc_register_settings');
function sswc_register_settings_page()
{
add_options_page('Social Sharing with Claps', 'SSWC Settings', 'manage_options', 'sswc', 'sswc_options_page');
}
//Content for settings page will go here
function sswc_options_page()
{
?>
<div class='settings-page-container'>
<h1>Social Sharing with Claps</h1>
<h2>Settings</h2>
<form method='post' action='options-general.php?page=sswc'>
<?php settings_fields('sswc_options_page');?>
<table>
<tr valign='top'>
<th scope='row'><label class="form-label" for='sswc_color_option'>Color Value</label></th>
<td><input type='text' id='sswc_color_option' name='sswc_color_option' value=<?php echo get_option('sswc_color_option');?>></td>
</tr>
<tr valign='top'>
<th scope='row'><label class="form-label" for='sswc_top_option'>Top Offset</label></th>
<td><input type='text' id='sswc_top_option' name='sswc_top_option' value=<?php echo get_option('sswc_top_option');?>></td>
</tr>
<tr valign='top'>
<th scope='row'><label class="form-label" for='sswc_left_option'>Left Offset</label></th>
<td><input type='text' id='sswc_left_option' name='sswc_left_option' value=<?php echo get_option('sswc_left_option');?>></td>
</tr>
</table>
<?php do_settings_sections( 'sswc_options_page' ); submit_button(); ?>
</form>
</div>
<?php
}
add_action('admin_menu', 'sswc_register_settings_page');
?>
The problem is that it won't save any changes I make in settings page. I'm suspicious its because the sswc_register_settings()
get called everytime an resets the values? Please help.
I'm creating a plugin which have few settings, I've created a settings page for it. Here's the code:
function sswc_register_settings()
{ echo "---------------------------------------------register_settings function called!";
add_option('sswc_color_option', '#A18DC6');
add_option('sswc_top_option', '400');
add_option('sswc_left_option', '100');
register_setting('sswc_options_group', 'sswc_color_option');
register_setting('sswc_options_group', 'sswc_top_option');
register_setting('sswc_options_group', 'sswc_left_option');
}
add_action('admin_init', 'sswc_register_settings');
function sswc_register_settings_page()
{
add_options_page('Social Sharing with Claps', 'SSWC Settings', 'manage_options', 'sswc', 'sswc_options_page');
}
//Content for settings page will go here
function sswc_options_page()
{
?>
<div class='settings-page-container'>
<h1>Social Sharing with Claps</h1>
<h2>Settings</h2>
<form method='post' action='options-general.php?page=sswc'>
<?php settings_fields('sswc_options_page');?>
<table>
<tr valign='top'>
<th scope='row'><label class="form-label" for='sswc_color_option'>Color Value</label></th>
<td><input type='text' id='sswc_color_option' name='sswc_color_option' value=<?php echo get_option('sswc_color_option');?>></td>
</tr>
<tr valign='top'>
<th scope='row'><label class="form-label" for='sswc_top_option'>Top Offset</label></th>
<td><input type='text' id='sswc_top_option' name='sswc_top_option' value=<?php echo get_option('sswc_top_option');?>></td>
</tr>
<tr valign='top'>
<th scope='row'><label class="form-label" for='sswc_left_option'>Left Offset</label></th>
<td><input type='text' id='sswc_left_option' name='sswc_left_option' value=<?php echo get_option('sswc_left_option');?>></td>
</tr>
</table>
<?php do_settings_sections( 'sswc_options_page' ); submit_button(); ?>
</form>
</div>
<?php
}
add_action('admin_menu', 'sswc_register_settings_page');
?>
The problem is that it won't save any changes I make in settings page. I'm suspicious its because the sswc_register_settings()
get called everytime an resets the values? Please help.
1 Answer
Reset to default 2--- You made a little mistake in code , I resolve this Error in the code So put this code ---
function sswc_register_settings()
{
register_setting('sswc_options_group', 'sswc_color_option');
register_setting('sswc_options_group', 'sswc_top_option');
register_setting('sswc_options_group', 'sswc_left_option');
}
add_action('admin_menu', 'sswc_register_settings_page');
function sswc_register_settings_page()
{
add_options_page('Social Sharing with Claps', 'SSWC Settings', 'manage_options', 'sswc', 'sswc_options_page');
add_action('admin_init', 'sswc_register_settings');
}
//Content for settings page will go here
function sswc_options_page()
{
?>
<div class='settings-page-container'>
<h1>Social Sharing with Claps</h1>
<h2>Settings</h2>
<form method='post' action='options.php'>
<?php
settings_fields('sswc_options_group');
do_settings_sections( 'sswc_options_group' );
?>
<table>
<tr valign='top'>
<th scope='row'><label class="form-label" for='sswc_color_option'>Color Value</label></th>
<td><input type='text' id='sswc_color_option' name='sswc_color_option' value=<?php echo get_option('sswc_color_option');?>></td>
</tr>
<tr valign='top'>
<th scope='row'><label class="form-label" for='sswc_top_option'>Top Offset</label></th>
<td><input type='text' id='sswc_top_option' name='sswc_top_option' value=<?php echo get_option('sswc_top_option');?>></td>
</tr>
<tr valign='top'>
<th scope='row'><label class="form-label" for='sswc_left_option'>Left Offset</label></th>
<td><input type='text' id='sswc_left_option' name='sswc_left_option' value=<?php echo get_option('sswc_left_option');?>></td>
</tr>
</table>
<?php submit_button(); ?>
</form>
</div>
<?php
}
Output :
[Refer This Link :] https://codex.wordpress/Creating_Options_Pages
本文标签: wp adminPlugin settings won39t save changes
版权声明:本文标题:wp admin - Plugin settings won't save changes 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744720064a2621643.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论