Getting settings saved two times in settings page with tab

I tried to show tabbed settings in admin settings page by a tutorial but for me am getting settings saved notification for two times.

I think it is due to different settings field but based on selection of tab am showing only one form using the php condition. So there might be only one form with one settings_fields() but how it is showing settings saved two times when i save. i don’t know any idea. can any body help on this.

Read More

Code

    <div class="wrap">  

        <div id="icon-themes" class="icon32"></div>  
        <h2>Sandbox Theme Options</h2>  
        <?php settings_errors(); ?>  
        <?php $active_tab = isset( $_GET[ 'tab' ] ) ? $_GET[ 'tab' ] : 'd_options';  ?>
        <h2 class="nav-tab-wrapper">  
    <a href="?page=followshape&tab=d_options" class="nav-tab <?php echo $active_tab == 'd_options' ? 'nav-tab-active' : ''; ?>">Display</a>  
    <a href="?page=followshape&tab=s_options" class="nav-tab <?php echo $active_tab == 's_options' ? 'nav-tab-active' : ''; ?>">Shape</a> 
    <a href="?page=followshape&tab=Stat" class="nav-tab <?php echo $active_tab == 'Stat' ? 'nav-tab-active' : ''; ?>">Stat</a> 
</h2>



            <?php  if( $active_tab == 'd_options' ) { ?>
        <form method="post" action="options.php">  
            <?php
            settings_fields( 'shapet' );  
            ?>

            <div class="show_type">
            <input type="radio" name="shape" id="radio1" value="1"<?php checked(1,get_option('shape')); ?>/>
            <label for="radio1"><img src="<?php echo plugins_url(); ?>/slider/images/1.jpg"></label>

            <input type="radio" id="radio2" name="shape" value="2"<?php checked(2,get_option('shape')); ?> />
            <label for="radio2"><img src="<?php echo plugins_url(); ?>/slider/images/2.jpg"></label>

            <input type="radio" id="radio3" name="shape" value="3"<?php checked(3,get_option('shape')); ?> />
            <label for="radio3"><img src="<?php echo plugins_url(); ?>/slider/images/3.jpg"></label>

            <input type="radio" id="radio4" name="shape" value="4"<?php checked(4,get_option('shape')); ?> />
            <label for="radio4"><img src="<?php echo plugins_url(); ?>/slider/images/4.jpg"></label>

            <input type="radio" id="radio5" name="shape" value="5"<?php checked(5,get_option('shape')); ?> />
            <label for="radio5"><img src="<?php echo plugins_url(); ?>/slider/images/5.jpg"></label>
            <input type="text" name="sh_color" value="<?php echo get_option('sh_color'); ?>" class="ir" />
            </div>
            <input type="submit" name="submit" /> 
            </form>  
            <?php 
        } if($active_tab == 's_options') { ?>
           <form method="post" action="options.php">
               <?php
            settings_fields( 'social' );
            for($i=1;$i<=8;$i++){
           ?>
            <lable>Social Network<?php echo $i; ?> </lable><input type="text" name="sfsocial_net<?php echo $i; ?>" value="<?php echo get_option('sfsocial_net'.$i); ?>"/>
            <lable>Show </lable><input type="checkbox" name="sfsocial_net_show<?php echo $i; ?>" value="1"<?php checked(1,get_option('sfsocial_net_show'.$i)); ?> />
            <lable>Lable</lable><input type="text" name="sfsocial_lable<?php echo $i; ?>" value="<?php echo get_option('sfsocial_lable'.$i); ?>"/>
            <br>

            <?php  }
               ?>
             <input type="submit" name="submit" /> 
          </form> 

        <?php } if ($active_tab == 'Stat') { ?>
            <form method="post" action="options.php">
                <?php
            echo "graph";
            settings_fields('stat');?>
            <lable>facebook</lable><input type="text" name="facebookun" value="<?php echo get_option('facebookun'); ?>"/>
             <input type="submit" name="submit" /> 
            </form>
        <?php
        }?>    



    </div>

Tried After posting Question from @MrJustin advice

After i came to know i should use add_settings_section and add_settings_field apart from register_setting

Here is the code i tried from another tutorial.

function registering_settings(){
  register_setting( 'my-settings-group', 'my-setting' );
    add_settings_section( 'section-one', 'Section One', 'section_one_callback', 'my-plugin' );
    add_settings_field( 'field-one', 'Field One', 'field_one_callback', 'my-plugin', 'section-one' );
}
add_action('admin_init','registering_settings');

function callback_testing(){
    ?>
<div class="wrap">  

        <div id="icon-themes" class="icon32"></div>  
        <h2>Sandbox Theme Options</h2>  
        <?php settings_errors(); ?>  
        <?php $active_tab = isset( $_GET[ 'tab' ] ) ? $_GET[ 'tab' ] : 'd_options';  ?>
        <h2 class="nav-tab-wrapper">  
    <a href="?page=slug_testing&tab=d_options" class="nav-tab <?php echo $active_tab == 'd_options' ? 'nav-tab-active' : ''; ?>">Display Options</a>  
    <a href="?page=slug_testing&tab=s_options" class="nav-tab <?php echo $active_tab == 's_options' ? 'nav-tab-active' : ''; ?>">Social Options</a> 
    <a href="?page=slug_testing&tab=S" class="nav-tab <?php echo $active_tab == 'S' ? 'nav-tab-active' : ''; ?>">Statistics</a> 
    <a href="?page=slug_testing&tab=new" class="nav-tab <?php echo $active_tab == 'new' ? 'nav-tab-active' : ''; ?>">New</a> 
</h2>

        <?php  if ($active_tab == 'new') { ?>
            <form action="options.php" method="POST">
            <?php settings_fields( 'my-settings-group' ); ?>
            <?php do_settings_sections( 'my-plugin' ); ?>
            <?php submit_button(); ?>
        </form>

        <?php } ?>
         <?php  if ($active_tab == 'd_options') { ?>

        <?php } ?>
         <?php  if ($active_tab == 's_options') { ?>

        <?php } ?>
    </div><!-- /.wrap -->

    <?php
}

  function section_one_callback() {
    echo 'Some help text goes here.';
}
  function field_one_callback() {
    $setting = esc_attr( get_option( 'my-setting' ) );
    echo "<input type='text' name='my-setting' value='$setting' />";
}

Even after trying this i am getting settings saved two times. But there is only one form section with one settings_fields and only one form in total. Why it is showing settings saved two times when i hit save changes.

Edit

  register_setting( 'my-plugin', 'my-setting' );
    add_settings_section( 'section-one', 'Section One', 'section_one_callback', 'my-plugin' );
    add_settings_field( 'field-one', 'Field One', 'field_one_callback', 'my-plugin', 'section-one' );


        <form method="post" action="options.php">  
<?php  if($active_tab == 'new') {
    settings_fields( 'my-plugin' );
    do_settings_sections( 'my-plugin' );
} else if($active_tab == 'd_options') { 
    // Do D_Options
} else if($active_tab == 's_options') {
    // Do S_Options
} 
submit_button();
?>  
</form>

Related posts

2 comments

  1. The culprit is:

    <?php settings_errors(); ?>
    

    It isn’t needed and will produce a second “Settings saved.” notification if there are no errors.

    Edited for formatting.

  2. Your going about setting up your options page in the non-traditional manner. View This WP/StackExchange correct answer, this is how you should have your settings page setup. This works flawlessly. *Note: My answer is the correct one.

    –Edit–

    Try this: your actual display is still a bit off, it should not save twice, I wrapped your form around your actual display content, and put your submit_button the content.

    <form method="post" action="options.php">  
    <?php  if($active_tab == 'new') {
        settings_fields( 'my-settings-group' );
        do_settings_sections( 'my-plugin' );
    } else if($active_tab == 'd_options') { 
        // Do D_Options
    } else if($active_tab == 's_options') {
        // Do S_Options
    } 
    submit_button();
    ?>  
    </form>
    

    –Another Edit–

    I did notice this:

        // Your Code(s)
    register_setting( 'my-settings-group', 'my-setting' );
    
    settings_fields( 'my-settings-group' );
    do_settings_sections( 'my-plugin' );
    
        // Should Be 
    
    register_setting('my-plugin', 'my-plugin');
    
    settings_fields( 'my-plugin' );
    do_settings_sections( 'my-plugin' );
    

    The last part of the add_settings_section() is the option group itself. So ‘my-plugin’ would be the group. The actual option would be the first part of add_settings_field()

    Meaning: my-plugin[‘field-one’] is the option.

    Go ahead and make these adjustments and see what you get.

    After that, post back with an update.

Comments are closed.