dotty
dotty

Reputation: 41463

Displaying settings options up using WordPress options API

I'm having issues getting my field to save using WordPresses setting API

I have 1 option page, with 1 section and 1 field. So it's pretty straight forward. Everything appearing/rendering fine, but I can't save the fields.

class LocalSEOAdmin {

    var $slug = 'local_seo_settings';

    var $name = "Local SEO";

    public function __construct(){

        // When WP calls admin_menu, call $this->admin_settings
        if ( is_admin() ) {
            add_action('admin_menu',  array( &$this, 'add_options_page' ) );  // on admin_menu call $this->init_admin_menu
            add_action( 'admin_init', array( &$this, 'add_setting_fields' ) );

        }

    }

    public function add_options_page(){
        add_options_page( 
            $this->name, // Page title
            $this->name, // Menu Title
            'manage_options',  // Role
            $this->slug, // Menu slug
            array( &$this, 'local_seo_admin_menu_callback' ) // Callback to render the option page
        );
    }

    public function local_seo_admin_menu_callback(){ 
        ?>
        <div class='wrap'>
        <h1><?php echo $this->name ?> Options</h1>
        <form method='post' action='options.php'>
        <?php
            settings_fields( 'address_settings_section' );
            do_settings_sections( $this->slug );
            submit_button();    
        ?>
        </form>
        </div>
        <?php

    } 

    public function add_setting_fields(){

        add_settings_section(
            'address_settings_section',         // ID used to identify this section and with which to register options
            'Address Options',      // Title to be displayed on the administration page
            array( &$this, '_render_address_options'),  // Callback used to render the description of the section
            $this->slug     // Page on which to add this section of options
        );

        add_settings_field( 
            'address_line_1',                       // ID used to identify the field throughout the theme
            'Address Line 1',                           // The label to the left of the option interface element
            array( &$this, '_render_address_line_1'),   // The name of the function responsible for rendering the option interface
            $this->slug,    // The page on which this option will be displayed
            'address_settings_section',         // The name of the section to which this field belongs
            array(                              // The array of arguments to pass to the callback. In this case, just a description.
                __( 'Activate this setting to display the header.', 'sandbox' ),
            )
        );

        register_setting(
            'address_settings_section',
            'address_settings_section'
        );

    }

    public function _render_address_options(){
        echo '<p>Add you address</p>';
    }

    public function _render_address_line_1(){
        $option = get_option( 'address_line_1' );
        $html = '<input type="text" id="address_line_1" name="address_line_1" value="' . $option . '" />'; 
        echo $html;
    }


}

new LocalSEOAdmin;

I'm not sure what's going on, but I feel I've mixed up the required fields someone. The fields are showing in WordPress and when I hit update the "setting saved." messages comes up, but nothing gets saved.

Upvotes: 0

Views: 259

Answers (2)

jay.jivani
jay.jivani

Reputation: 1574

In register_setting()

First paramerter is your option group name &
Second parameter is option name which is given in add_settings_field() , in your case is should be address_line_1 not address_settings_section

Finally

register_setting(
                'address_settings_section',
                'address_line_1'
                )

Upvotes: 0

Kiyan
Kiyan

Reputation: 2193

there is just a little problem when you register your setting.

base on register_setting, the second parameter, is $option_name so you must put send name of you inputs

    register_setting(
        'address_settings_section',
        'address_line_1'
    );

and now it's worked.

you can find more about creating options pages in wordpress site.

Upvotes: 0

Related Questions