Wordpress 4.1设置页面只保存一个字段

时间:2015-03-22 16:47:46

标签: php wordpress-plugin wordpress

我现在有点迷失,因为我正在为我的插件执行设置页面,但只有一个字段存储到数据库中。我不确定如何追踪问题。

这是我的代码

<?php

// Class implementing all of the necessary steps for adding a settings page in Wordpress
class EventsSettings {

    // The text that identifies settings as belonging to this plugin
    const PLUGIN_SLUG = "events";
    // The group that all of the Podio settings will belong to
    const PODIO_SETTINGS_GROUP = "events-podio-settings";
    const PODIO_SETTINGS_SECTION = "events-podio-settings-section";

    // The names of the settings that will be stored
    const OPT_PODIO_CLIENT_ID = "events-podio-client-id";
    const OPT_PODIO_CLIENT_SECRET = "events-podio-client-secret";
    const OPT_PODIO_APP_ID = "events-podio-app-id";
    const OPT_PODIO_APP_TOKEN = "events-podio-app-token";

    const FACEBOOK_SETTINGS_GROUP = "events-facebook-settings";
    const FACEBOOK_SETTINGS_SECTION = "events-facebook-settings-section";

    const OPT_FACEBOOK_APP_ID = "events-facebook-app-id";

    public function __construct() {
        // Adds hooks for functions that get called to add a menu item, render a settings page and 
        // register settings that we will store
        add_action("admin_menu", array($this, "initAdminMenu"));
        add_action("admin_init", array($this, "initSettingsOptions"));
    }

    // Handles adding a menu item to the Wordpress settings list
    public function initAdminMenu() {
        // Adds a menu item that will call renderOptionsPage when opened
        add_options_page("New York Events", " Events", "manage_options", self::PLUGIN_SLUG, array($this, "renderOptionsPage"));
    }

    // Initializes all settings that we will store
    public function initSettingsOptions() {
        // Registers all settings with the same settings group
        register_setting(self::PODIO_SETTINGS_GROUP, self::OPT_PODIO_CLIENT_ID);
        register_setting(self::PODIO_SETTINGS_GROUP, self::OPT_PODIO_CLIENT_SECRET);
        register_setting(self::PODIO_SETTINGS_GROUP, self::OPT_PODIO_APP_ID);
        register_setting(self::PODIO_SETTINGS_GROUP, self::OPT_PODIO_APP_TOKEN);
        register_setting(self::FACEBOOK_SETTINGS_GROUP, self::OPT_FACEBOOK_APP_ID);

        // Creates a section of settings for the UI, then adds that section to the same
        // "plugin slug" that we used for creating the options page
        add_settings_section(self::PODIO_SETTINGS_SECTION, "Podio Settings", array($this, "podioSettingsHelpText"), self::PLUGIN_SLUG);

        // Add the actual input fields to the settings section that we just created.
        // The third parameter of each function call will get called to render the input field for 
        // the corresponding setting value
        add_settings_field(self::OPT_PODIO_CLIENT_ID, "Podio Client ID", array($this, "field_podioClientID"), self::PLUGIN_SLUG, self::PODIO_SETTINGS_SECTION);
        add_settings_field(self::OPT_PODIO_CLIENT_SECRET, "Podio Client Secret", array($this, "field_podioClientSecret"), self::PLUGIN_SLUG, self::PODIO_SETTINGS_SECTION);
        add_settings_field(self::OPT_PODIO_APP_ID, "Podio App ID", array($this, "field_podioAppID"), self::PLUGIN_SLUG, self::PODIO_SETTINGS_SECTION);
        add_settings_field(self::OPT_PODIO_APP_TOKEN, "Podio App Token", array($this, "field_podioAppToken"), self::PLUGIN_SLUG, self::PODIO_SETTINGS_SECTION);

        add_settings_section(self::FACEBOOK_SETTINGS_SECTION, "Facebook Settings", array($this, "facebookSettingsHelpText"), self::PLUGIN_SLUG);
        add_settings_field(self::OPT_FACEBOOK_APP_ID, "Facebook App ID", array($this, "field_facebookAppId"), self::PLUGIN_SLUG, self::FACEBOOK_SETTINGS_SECTION);
    }

    // This renders the HTML of our settings page
    public function renderOptionsPage() {
        $cache = FSCache::instance();
        // Uses the EventsTemplate helper to render the "settings" view
        EventsTemplate::render("settings", array('this' => $this, 'cache' => $cache));
    }

    // Returns help text for the settings page
    public function podioSettingsHelpText() {
        echo "These settings are used to get events from Podio";
    }

    public function facebookSettingsHelpText() {
        echo "These settings are used to share events to Facebook";
    }

    // Called for each setting to render an input field
    public function field_podioClientID() {
        echo $this->renderInput(self::OPT_PODIO_CLIENT_ID);
    } 
    public function field_podioClientSecret() {
        echo $this->renderInput(self::OPT_PODIO_CLIENT_SECRET);        
    } 
    public function field_podioAppID() {
        echo $this->renderInput(self::OPT_PODIO_APP_ID);
    } 
    public function field_podioAppToken() {
        echo $this->renderInput(self::OPT_PODIO_APP_TOKEN);
    }
    public function field_facebookAppId() {
        echo $this->renderInput(self::OPT_FACEBOOK_APP_ID);
    }

    // Renders the input field for a given option name
    private function renderInput($optionName) {
        // Gets the value of the option and then returns the HTML for the input
        $setting = esc_attr(get_option($optionName));
        return "<input type='text' id='$optionName' name='$optionName' value='$setting' style='width:400px;'/>";
    }

    // Helper function for getting the value of an option
    public function get($optionName) {
        $val = get_option($optionName);
        return empty($val) ? null : $val;
    }
}

这是我的模板

<div class="wrap">
    <h2>New York Events Plugin</h2>
    <form action="options.php" method="POST">
        <?php settings_fields($this::PODIO_SETTINGS_GROUP);?>
        <?php settings_fields($this::FACEBOOK_SETTINGS_GROUP);?>
        <?php do_settings_sections($this::PLUGIN_SLUG);?>
        <?php submit_button();?>
    </form>

    <h3>Cache Settings</h3>
    <div>
        Current Cache Size: <?php echo $cache->getCacheSize()?>
    </div>
    <div style="margin: 20px 0; width:620px;">
        If you've made changes to events in Podio and they're not showing up on the site, or if the cache is taking up too much space, you can clear the cache.
    </div>
    <div class="button button-primary">Clear Cache</div>
</div>

这是在我的设置页面上呈现的内容

enter image description here

如您所见,只有Facebook API存储在数据库中。

1 个答案:

答案 0 :(得分:0)

我发现有多个{{h1}}不起作用。所以在settings_fields我使用相同的组名,现在所有字段都正确更新。

我从http://www.mendoweb.be/blog/wordpress-settings-api-multiple-sections-on-same-page/

获得了它