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