Plugin forms overwrite each other’s options

Question

I am creating my first WP plugin, where I have two forms. Both work correctly, but when I save the information from one, the information from the other disappears.

I’d like to save all options in one array, and use the same group of options for both, but I don’t know if it’s possible.

main plugin php:

<?php

    /*
        Plugin Name: Team-K Shonichi
        ... 
    */

    // DIE
    if ( ! defined( 'ABSPATH' ) ) {
        die( '-1' );
    }

    // Paths
    define("TKSNC_DIR_PATH", dirname(__FILE__));            
    define("TKSNC_URL_PATH", plugin_dir_url( __FILE__ ));   
    define("TKSNC_DIR_NAME", basename(TKSNC_DIR_PATH));     

    // Includes
    include_once TKSNC_DIR_PATH . "/inc/tks-create-menus.php";
    include_once TKSNC_DIR_PATH . "/inc/tks-dashboard-tab.php";     // One form is here
    include_once TKSNC_DIR_PATH . "/inc/tks-breadcrumbs-tab.php";   // Other form is here
    include_once TKSNC_DIR_PATH . "/inc/tks-breadcrumbs-code.php";
    include_once TKSNC_DIR_PATH . "/inc/tks-admin-scripts.php";
    include_once TKSNC_DIR_PATH . "/inc/tks-misc-options.php";

    // Register Settings

    add_action( 'admin_init', function() {

        $args = array(
            'type' => 'array',
            'default' => null
        );
    
        register_setting( 'we_are_the_team_k', 'TK_Shonichi_Options', $args );

        if(empty(get_option('TK_Shonichi_Options'))) {
            add_option( 'TK_Shonichi_Options', array() );
        }
    
    });

?>

tks-dashboard-tab.php

<?php

    function TKS_Dashboard_Output() { ?>

        <div class="">
            <form action="options.php" method="post">
                <?php

                    settings_fields( 'we_are_the_team_k' );
                    do_settings_sections( 'we_are_the_team_k' );

                    $tk = get_option('TK_Shonichi_Options');
                                                                            
                ?>

                <table class="form-table tkTable" role="presentation">

                    <tbody>
                            
                        <tr>
                            <th scope="row" colspan="2" class="topPD">
                                <strong>
                                    <?php esc_html_e( 'Misc options:', 'teamk-shonichi' ); ?>
                                </strong>
                            </th>
                        </tr>
                    
                        <tr>
                            <th scope="row">
                                <label for="TK_Shonichi_Options[catImageField]">
                                    <strong>
                                        <?php esc_html_e( 'Add category image field', 'teamk-shonichi' ); ?>
                                    </strong>
                                </label>
                            </th>
                            <td>
                                <label class="switch">
                                    <input id="TK_Shonichi_Options[catImageField]" type="checkbox" name="TK_Shonichi_Options[catImageField]"<?php echo checked( isset($tk['catImageField']), true, false ); ?>>
                                    <span class="slider round"></span>
                                </label>                                
                            </td>
                        </tr>

                    </tbody>

                </table>
                            
                <?php submit_button( 'Save changes', 'primary', 'saveButton', true, null); ?>
     
            </form>

        </div>

    <?php }

?>

tks-breadcrumbs-tab.php

<?php

    // Create the Breadcrumbs menu page

    function TKS_BreadCrumbs_Output() {

            ?>

            <div class="generalWrp">

                <form action="options.php" method="post">
     
                    <?php

                        settings_fields( 'we_are_the_team_k' );
                        do_settings_sections( 'we_are_the_team_k' );

                        $tk = get_option('TK_Shonichi_Options');

                        // echo '<pre>'; print_r($tk); echo '</pre>';                             
                                                                            
                    ?>

                    <table class="form-table tkTable" role="presentation">

                        <tbody>

                            <tr>
                                <th scope="row">
                                    <label for="TK_Shonichi_Options[enable]">
                                        <strong>
                                            <?php esc_html_e( 'Enable Breadcrumbs', 'teamk-shonichi' ); ?>
                                        </strong>
                                    </label>
                                </th>
                                <td>
                                    <label class="switch">
                                        <input id="TK_Shonichi_Options[enable]" type="checkbox" name="TK_Shonichi_Options[enable]"<?php echo checked( isset($tk['enable']), true, false ); ?>>
                                        <span class="slider round"></span>
                                    </label>
                                </td>
                            </tr>

                            rest of options goes here....

                        </tbody>

                    </table>
                            
                    <?php submit_button( 'Save changes', 'primary', 'saveButton', true, null); ?>
     
                </form>

            </div>

            <?php

    }

So, when I save the form in tks-dashboard-tab.php the array is:

Array
(
    [catImageField] => on
)

When I save the form in tks-breadcrumbs-tab.php, the values are saved, but the previous value disappear:

Array
(
    [enable] => on
    [type] => WebSite
    [locale] => en_US
    [friendly] => on
    [catImage] => 
)

If I save the firts tab, once again:

Array
(
    [catImageField] => on
)

Can’t I have the same options for both forms?

I’m missing something, or is something bad in the code?

Thanks.

0
Rufi 2 weeks 2023-03-13T12:57:11-05:00 0 Answers 0 views 0

Leave an answer

Browse
Browse