2

Im trying to learn Settings API and I'd appreciate if someone could clarify this for me.

I want to store serialized data in the database. Im using a class to do all the work for me.

Heres what I have:

public function admin_init()
    {
        register_setting(
            'wp_plugin_template-group', 
            'my_settings', 
            array(&$this,'validate'));

        // add your settings section 
        add_settings_section(
            'wp_plugin_template-section', 
            'MY Settings', 
            array(&$this, 'settings_section_wp_plugin_template'), 
            'wp_plugin_template' );


        // add your setting's fields 
        add_settings_field(
            'wp_plugin_template-setting_a', 
            'Setting A', 
            array(&$this, 'settings_field_input_text'), 
            'wp_plugin_template', 
            'wp_plugin_template-section', 
            array( 'field' => 'setting_a' )
        );


        add_settings_field(
            'wp_plugin_template-setting_b', 
            'Setting B', 
            array(&$this, 'settings_field_input_text'), 
            'wp_plugin_template', 
            'wp_plugin_template-section', 
            array( 'field' => 'setting_b' )
        );

    }

As you can see, Im using validate callback to return an array so it can be serialized and stored in the DB. Heres the Validate function:

public function validate($args)
    {
        //echo "vali";      

        $valid = array();

        //$valid['setting_a'] = sanitize_text_field($args['setting_a']);
        //$valid['setting_b'] = sanitize_text_field($args['setting_b']);

        $valid['setting_a'] = $_POST['setting_a'];
        $valid['setting_b'] = $args['setting_b'];

        //$valid['setting_a'] = "AAAA";
        $valid['setting_b'] = "BBBB";

        return $valid;
    }

Please note only one value is now stored in the DB:

 $valid['setting_a'] = $_POST['setting_a']; //stored in the DB
 $valid['setting_b'] = $args['setting_b']; //Stored as null, in other words it doesnt work
 $valid['setting_b'] = "BBBB"; //Works fine, using it, just for testing purposes

This is how the form is built:

public function settings_field_input_text($args) 
    {
        // Get the field name from the $args array 
        $field = $args['field']; 

        // Get the value of this setting 
        $value = get_option($field);

        // echo a proper input type="text"
        echo sprintf(
           '<input type="text" name="%s" id="%s" value="%s" />', 
           $field, $field, $value); 

    }

And Displayed:

<div class="wrap">
<?php if ( isset( $_GET['settings-updated'] ) ) {
    echo "<div class='updated'><p>Settings updated successfully.</p></div>";
      } ?>
<?php screen_icon(); ?>
<h2>WP Plugin Template</h2> 
    <form method="post" action="options.php">
    <?php //Output nonce, action, and option_page fields for a settings page. Please note that this function must be called inside of the form tag for the options page. ?>
        <?php @settings_fields('wp_plugin_template-group'); ?> 
        <?php @do_settings_fields('wp_plugin_template-group'); ?> 
        <?php @do_settings_sections('wp_plugin_template'); ?> 
        <?php @submit_button(); ?> 
    </form> 
</div>

HTML Version A:

<div class="wrap">
    <div class="icon32" id="icon-usy-settings"><br></div>   
    <h2>WP Plugin Template</h2> 
    <form action="options.php" method="post">
        <input type="hidden" value="wp_plugin_template-group" name="option_page">
        <input type="hidden" value="update" name="action">
        <input type="hidden" value="5d72478b96" name="_wpnonce" id="_wpnonce">
        <input type="hidden" value="/inspired/wp-admin/admin.php?page=usy-settings" name="_wp_http_referer"> 

        <h3>MY Settings</h3>
        These settings do things for the WP Plugin Template.

        <table class="form-table">
            <tbody>
                <tr valign="top">
                    <th scope="row">Setting A</th>
                    <td><input type="text" value="" id="setting_a" name="setting_a"></td>
                </tr>
                <tr valign="top">
                    <th scope="row">Setting B</th>
                    <td><input type="text" value="" id="setting_b" name="setting_b"></td>
                </tr>
            </tbody>
        </table> 
        <p class="submit">
            <input type="submit" value="Save Changes" class="button button-primary" id="submit" name="submit">
        </p> 
    </form> 
</div>

And also I've tried this:

HTML Version B:

<div class="wrap">
    <div class="icon32" id="icon-usy-settings"><br></div>
    <h2>WP Plugin Template</h2>
    <div class="updated"><p>Settings updated successfully.</p></div> 
    <form action="options.php" method="post">
        <input type="hidden" value="wp_plugin_template-group" name="option_page">
        <input type="hidden" value="update" name="action">
        <input type="hidden" value="5d72478b96" name="_wpnonce" id="_wpnonce">
        <input type="hidden" value="/inspired/wp-admin/admin.php?page=usy-settings&amp;settings-updated=true" name="_wp_http_referer"> 

        <h3>MY Settings</h3> 
        These settings do things for the WP Plugin Template.
        <table class="form-table">
            <tbody>
                <tr valign="top">
                    <th scope="row">Setting A</th>
                    <td><input type="text" value="" id="setting_a" name="field[setting_a]"></td>
                </tr>
                <tr valign="top">
                    <th scope="row">Setting B</th>
                    <td><input type="text" value="" id="setting_b" name="field[setting_b]"></td>
                </tr>
            </tbody>
        </table> 
        <p class="submit">
            <input type="submit" value="Save Changes" class="button button-primary" id="submit" name="submit">
        </p> 
    </form> 
</div>

I was wondering if there is a better way of doing this? I was under impression that all the form values will be passed as parameter $args

But no matter what I try, $args always empty:(

Can you please help me understand how to get it to work? or is $_POST['setting_a'] my only option?

  • Add the form fields to your question. – fuxia Apr 3 '13 at 3:50
  • Thank you Toscho, I updated my question as per your request. – user30899 Apr 3 '13 at 5:09
  • What is the actual name attribute of your input fields? – fuxia Apr 3 '13 at 7:18
  • I have seen it; just haven't found the time to answer. – fuxia Apr 4 '13 at 5:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.