admin管理员组文章数量:1287791
I'm trying to put up a simple plugin, but this Settings API doesn't give me a break.
I have one field registered in the settings page, but the values we enter don't get saved to the database.
Here is the code I've put together so far. Any help is highly appreciated
<?php
if(is_admin()){
add_action( 'admin_menu', 'my_plugin_menu' );
add_action( 'admin_init', 'register_mysettings' );
}
function register_mysettings() {
register_setting( 'purchase-history-grid', 'num_of_columns' );
add_settings_section(
'plugin_section',
__( 'The Grid', 'wordpress' ),
'settings_section_callback',
'purchase-history-grid'
);
add_settings_field(
'num_of_columns',
'Number of Columns',
'show_num_of_cols',
'purchase-history-grid',
'plugin_section'
);
}
function my_plugin_menu() {
add_options_page( 'Purchase History Grid', 'Purchase History Grid', 'manage_options', 'purchase-history-grid', 'my_plugin_options' );
}
function my_plugin_options() {
if ( !current_user_can( 'manage_options' ) ) {
wp_die( __( 'You do not have sufficient permissions to access this page.' ) );
}
?>
<div class="wrap">
<h2>Purchase History Grid by SolutionsW3</h2>
<form method="post" action="options.php">
<?php
settings_fields( 'purchase-history-grid' );
do_settings_sections( 'purchase-history-grid' );
submit_button();
?>
</form>
</div>
<?php
}
function show_num_of_cols() {
// get the value of the setting we've registered with register_setting()
$setting = get_option('num_of_cols');
// output the field
?>
<input type="text" id="num_of_cols" name="num_of_cols" value="<?php echo isset( $setting ) ? esc_attr( $setting ) : ''; ?>">
<?php
}
function settings_section_callback( ) {
echo __( 'Please configure the grid here', 'wordpress' );
}
?>
I'm trying to put up a simple plugin, but this Settings API doesn't give me a break.
I have one field registered in the settings page, but the values we enter don't get saved to the database.
Here is the code I've put together so far. Any help is highly appreciated
<?php
if(is_admin()){
add_action( 'admin_menu', 'my_plugin_menu' );
add_action( 'admin_init', 'register_mysettings' );
}
function register_mysettings() {
register_setting( 'purchase-history-grid', 'num_of_columns' );
add_settings_section(
'plugin_section',
__( 'The Grid', 'wordpress' ),
'settings_section_callback',
'purchase-history-grid'
);
add_settings_field(
'num_of_columns',
'Number of Columns',
'show_num_of_cols',
'purchase-history-grid',
'plugin_section'
);
}
function my_plugin_menu() {
add_options_page( 'Purchase History Grid', 'Purchase History Grid', 'manage_options', 'purchase-history-grid', 'my_plugin_options' );
}
function my_plugin_options() {
if ( !current_user_can( 'manage_options' ) ) {
wp_die( __( 'You do not have sufficient permissions to access this page.' ) );
}
?>
<div class="wrap">
<h2>Purchase History Grid by SolutionsW3</h2>
<form method="post" action="options.php">
<?php
settings_fields( 'purchase-history-grid' );
do_settings_sections( 'purchase-history-grid' );
submit_button();
?>
</form>
</div>
<?php
}
function show_num_of_cols() {
// get the value of the setting we've registered with register_setting()
$setting = get_option('num_of_cols');
// output the field
?>
<input type="text" id="num_of_cols" name="num_of_cols" value="<?php echo isset( $setting ) ? esc_attr( $setting ) : ''; ?>">
<?php
}
function settings_section_callback( ) {
echo __( 'Please configure the grid here', 'wordpress' );
}
?>
Share
Improve this question
edited Sep 10, 2021 at 12:56
Sally CJ
40.1k2 gold badges28 silver badges49 bronze badges
asked Sep 10, 2021 at 11:49
DesperDesper
133 bronze badges
1 Answer
Reset to default 1The option value is not being saved in the database because, as you could see below, the registered option name is num_of_columns
and not num_of_cols
:
// The second parameter is the option name.
register_setting( 'purchase-history-grid', 'num_of_columns' );
So make sure that you use the correct option name in your settings field callback:
Use
get_option( 'num_of_columns' )
when retrieving the saved option, andUse
name="num_of_columns"
in the<input>
本文标签: Option value not get saved in the database
版权声明:本文标题:Option value not get saved in the database 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741328009a2372602.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论