WordPress 4.1插件仅将1个字段保存到数据库

时间:2015-03-22 作者:toy

我现在有点不知所措,因为我正在为我的插件创建设置页面,但数据库中只存储了一个字段。我不知道如何找出这个问题。

这是我的密码

<?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 个回复
SO网友:toy

我发现有很多settings_fields 这行不通。所以在register_setting 我使用了相同的组名,现在所有字段都已正确更新。

我从这里得到的http://www.mendoweb.be/blog/wordpress-settings-api-multiple-sections-on-same-page/

结束

相关推荐

在加载plugins_后,get_plugins()不工作

知道为什么下面的代码function my_plugin_load() { get_plugins(); } add_action( \'plugins_loaded\', \'my_plugin_load\' ); 抛出此错误?Fatal error: 不应调用未定义的函数get\\u plugins()get_plugins() 定义在plugins_loaded 胡克开火了?如果不是,那么什么才是合适的钩子呢?(这个钩子应该启动插件的引导/加载过程)