0

I have created the following custom menu page to save some data, I've included some repeatable fields inside the form. The problem that I have is that every time I click the submit button it overrides my previous values in the array. Is there a way to prevent this?

I've tried the add_option(); function instead of update_option(); without any success.

//Register Custom Admin Menu 

function add_all_teams()
    {

        add_menu_page(
            "All Teams",
            "All Teams",
            "manage_options", 
            "all-teams", 
            "all_teams_page", /
            "", 
            100
        );
}

add_action("admin_menu", "add_all_teams");



    function all_teams_page() {


        if (isset($_POST['all_teams'])) {
            $value = $_POST['all_teams'];
            add_option('all_teams', $value); //update_option('all_teams', $value);
        }

        $allTeamOptions = get_option('all_teams');

            echo '<div class="wrap"><h1>All Teams</h1> <form method="POST">';

            echo '<table class="form-table" role="presentation"><tbody><tr><th scope="row">My Teams</th><td>';
            echo '<ul id="tracks-repeatable" class="custom_repeatable"><a class="repeatable-add button" href="#">+</a>';

                if ( ! empty( $allTeamOptions ) ) {
                    $i = 0;

                    foreach( $allTeamOptions['allTeams'] as $allTeamOption ) {
                        echo 
                        '<li style=""><span class="sort hndle ui-sortable-handle">|||</span>
                        <input type="text" name="all_teams[allTeams]['.$i.'][team][name]" value="' . $allTeamOption['team']['name'] .'" size="10" />
                        <a class="repeatable-remove button" href="#">-</a>
                        </li>';

                        $i++;
                    }
                    } else {
                        echo 
                        '<li style=""><span class="sort hndle ui-sortable-handle">|||</span>
                        <input type="text" name="all_teams[allTeams][0][team][name]" value="" size="10" />
                        <a class="repeatable-remove button" href="#">-</a>
                        </li>';
                    }

            echo '</ul></td></tr></tbody></table>' ;
            echo '<input type="submit" value="Save" class="button button-primary button-large">
                 </form></div>';

    }

Thank you

3
  • admin_menu should be used to add/register custom admin menus (e.g. using add_menu_page()) and not directly echo a settings page. Can you show your full code?
    – Sally CJ
    Commented May 22, 2020 at 23:33
  • @SallyCJ I've updated the post with the full code.
    – trunks
    Commented May 23, 2020 at 5:37
  • You're not actually using the Settings API. You've just added an admin menu page. Please see: developer.wordpress.org/plugins/settings/settings-api Commented May 23, 2020 at 6:21

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.