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
Add a comment  | 

1 Answer 1

Reset to default 1

The 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, and

  • Use name="num_of_columns" in the <input>

本文标签: Option value not get saved in the database