1

I have created plugin that have the settings, and then I want to duplicate that settings for other tab with same form but the process of I/O will independent each other.

Here's the code so far:

    function __construct( $prefix )
    {
        $this->setttings_prefix = $prefix;

        if(  wp_verify_nonce($_POST['save_settings_field'], 'save_settings_action') )
        {
            $options = array();
            foreach( $_POST as $key=>$value )
            {
                $options[$key] = $value;
            }
            update_option( $this->setttings_prefix.'_options', $options );

            $this->message = '<div class="alert alert-success">Settings saved</div>';
        }

    }


    function show_settings()
            {
                $current_page = isset($_GET['page']) ? $_GET['page'] : '';
                $active_tab = isset( $_GET[ 'tab' ] ) ? $_GET[ 'tab' ] : 'settings1';
                ?>

                <div class="wrap tw-bs4">

                    <h2 class="nav-tab-wrapper">
                        <a href="?page=wsr_settings&tab=settings1" class="nav-tab  <?php echo $active_tab == "settings1" ? "nav-tab-active" : ""; ?>"><?php _e('Settings1', 'sc'); ?></a>
                        <a href="?page=wsr_settings&tab=settings2" class="nav-tab  <?php echo $active_tab == "settings2" ? "nav-tab-active" : ""; ?>"><?php _e('Settings2', 'sc'); ?></a>
                    </h2>

                    <hr/>

                    <?php
                    if( $active_tab == 'settings1' )
                    {
                    ?>
                        <form class="form-horizontal" method="post" action="">
                            <?php
                                wp_nonce_field( 'save_settings_action', 'save_settings_field'  );
                                $config = get_option( $this->setttings_prefix.'_options1');
                            ?>

                            <fieldset>
                                <?php
                                    foreach( $this->setttings_parameters as $single_page )
                                    {
                                        $hidden_class = 'hidden';
                                        if($single_page['menu_slug'] == $current_page)
                                        {
                                            $hidden_class = '';
                                        }
                                        foreach( $single_page['parameters'] as $key=>$value )
                                        {
                                            switch( $value['type'] )
                                            {
                                                case "separator":
                                                $out .= '
                                                <div class="lead">'.$value['title'].'</div> 
                                                ';
                                                break;
                                                case "text":
                                                $out .= '
                                                <div class="form-group '.$hidden_class.'">
                                                <label class="control-label" for="'.$value['id'].'">'.$value['title'].'</label>
                                                <input type="text"  class="form-control '.$value['class'].'"  name="'.$value['name'].'" id="'.$value['id'].'" placeholder="'.$value['placeholder'].'" value="'.esc_html( stripslashes( $config[$value['name']] ) ).'">
                                                <p class="help-block">'.$value['sub_text'].'</p>
                                                </div>
                                                ';
                                                break;
                                                case "hidden":
                                                $out .= '<input type="hidden"  name="'.$value['name'].'" value="'.esc_html( stripslashes( $config[$value['name']] ) ).'">';
                                                break;
                                                case "button":
                                                $out .= '
                                                <div class="form-group '.$hidden_class.'">
                                                <label class="control-label" for="">&nbsp;</label>
                                                <a class="btn btn-success" href="'.$value['href'].'"   >'.$value['title'].'</a>
                                                </div>
                                                ';
                                                break;
                                                $out .= '
                                            </fieldset>
                                            </div>
                                            ';
                                            break;
                                            }
                                        }
                                    }
                                    echo $out;
                                ?>

                                <div class="form-actions">  
                                    <button type="submit"   class="btn btn-primary">Save Settings</button>  
                                </div>  

                            </fieldset>  
                        </form>
                        <?php
                        echo $this->message;
                    ?>

                </div>
                <?php
                }
                elseif( $active_tab == 'settings2' )
                {
                    .......Same as above......
                }
            }
        }

The code is succesffuly show multi tab and form but it still show same settings and I/O for each Setting Tab. Please help me. Thanks in advanced.

1

Essentially what you can do is add an identifier for each 'tab' and then save it in a multidimensional array, this is a neat trick that inputs can do and works great for WordPress plugins that uses tabs.

<form id="form_1">
 <div class="tab_1">
  <input type="text" name="tab1['fieldname_0']" value="tab_1_0">
  <input type="text" name="tab1['fieldname_1']" value="tab_1_1">
  <input type="text" name="tab1['fieldname_2']" value="tab_1_2">
  <input type="text" name="tab1['fieldname_3']" value="tab_1_3">
 </div>

 <div class="tab_2">
  <input type="text" name="tab2['fieldname_0']" value="tab_2_0">
  <input type="text" name="tab2['fieldname_1']" value="tab_2_1">
  <input type="text" name="tab2['fieldname_2']" value="tab_2_2">
  <input type="text" name="tab2['fieldname_3']" value="tab_2_3">
 </div>
</form>

Your PHP wont need to change, but your return data when calling get_option() will change to an array which is easy to iterate over from there or reference by $key.

  • Thank you Johan, I will try your answer. If it succesfull then I will accept your answer. – pije76 Oct 9 '19 at 5:43
  • @pije76 No problem, i see that you redirect to a new page when changing tabs, so you will need to modify your code a bit or modify my code a bit. In your instance you can use my tip and append to the settings array, which seems to be what you are trying to do. – Johan Pretorius Oct 9 '19 at 8:49

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.