最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

database - WordPress Settings API Overrides My Previous Value

programmeradmin0浏览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

发布评论

评论列表(0)

  1. 暂无评论