设置接口:保存设置后有两个更新通知?

时间:2013-11-06 作者:David Kees

我使用设置API为插件设置创建了一个非常简单的页面。这是我第一次使用API,通过各种在线教程和这里的答案,我想我已经找到了答案。事实证明,这个页面甚至可以工作!!

然而,奇怪的事情正在发生:

two notices regarding the saving of settings

如上所示,屏幕上显示了两条div.update消息。我查过了,只有一个电话settings_errors() 函数so和该函数没有在循环中调用或我能找到的任何东西,但我仍然收到两条通知。

代码如下:

public function __construct() {
    /* more stuff */

    add_action("admin_menu", array($this, "plugin_settings_menu_add");
    if($pagenow == "options.php" || $pagenow == "options-general.php") {
        add_action("admin_init", array($this, "plugin_settings_initialize");
    }
}

public function plugin_settings_menu_add() {
    add_options_page("Gallery Settings", "Galleries", "manage_options", "galleries-settings-page", array($this, "plugin_settings_menu_display"));
}

public function plugin_settings_menu_display() {
    require(plugin_dir_path(__FILE__) . "views/plugin-settings.php");
}

public function plugin_settings_permissions_to_use_default() {
    return apply_filters(
        "plugin_settings_permissions_to_use_default",
        array(
            "galleries-settings-permission-to-use-url" => ""
        )
    );
}

public function plugin_settings_initialize() {
    if(get_option("galleries-settings-permission-to-use") == false) {
        add_option("galleries-settings-permission-to-use", $this->plugin_settings_permissions_to_use_default());
    }

    add_settings_section(
        "galleries-settings-permission-to-use-section",                    // DOM ID for the section
        "Permission to Use Gallery Items",                                        // on-screen title for it
        array($this, "plugin_settings_permissions_to_use_header_display"),        // callback to display its header
        "galleries-settings-page"                                            // the menu page on which to show it
    );

    add_settings_field(
        "galleries-settings-permission-to-use-url",
        \'<label for="galleries-settings-permission-to-use-url">Webtools Form (required)</label>\',
        array($this, "plugin_settings_permissions_to_use_url_display"),
        "galleries-settings-page",
        "galleries-settings-permission-to-use-section"
    );

    register_setting(
        "galleries-settings-page",
        "galleries-settings-permission-to-use",
        array($this, "plugin_settings_permissions_to_use_sanitize")
    );
}

public function plugin_settings_permissions_to_use_header_display() {
    echo "<p>......</p>";
}

public function plugin_settings_permissions_to_use_url_display() {
    $settings = get_option("galleries-settings-permission-to-use");
    $value = isset($settings["galleries-settings-permission-to-use-url"])
        ? $settings["galleries-settings-permission-to-use-url"]
        : ""; ?>

    <input type="url" id="galleries-settings-permission-to-use-url"
        name="galleries-settings-permission-to-use[galleries-settings-permission-to-use-url]"
        value="<?=esc_url($value)?>" class="regular-text" required aria-required="true"><br>

    <em>Enter the full URL but without any query string parameters.</em>    
<? }

public function plugin_settings_permissions_to_use_sanitize($input) {
    $output = array();
    foreach($input as $key => $value) {
        // even though we have only one field here at the moment, we\'ll use a loop and a switch
        // to santize.  that way we\'re ready to go with respect to additional data that might one
        // day be sent here.  

        switch($key) {
        case "galleries-settings-permission-to-use-url":
            if(empty($value) || !filter_var($value, FILTER_VALIDATE_URL)) {
                add_setting_error("galleries-settings-permission-to-use-url",
                    "galleries-settings-permission-to-use-url-invalid",
                    "You must enter a valid web address."
                );
            } elseif(($qloc = strpos($value, "?"))!==false) $value = substr($value, 0, $qloc);
            break;
        }

        $output[$key] = $value;
    }

    return apply_filters("galleries_plugin_settings_permissions_to_use_sanitize", $output, $input);
}
以下是plugin-settings.php 上述代码中包含的页面:

<div class="wrap">
    <div id="icon-options-general" class="icon32"></div>
    <h2>Gallery Settings</h2>

    <? settings_errors(); ?>

    <form method="post" action="options.php">
            <? settings_fields("galleries-settings-page"); ?>
            <? do_settings_sections("galleries-settings-page"); ?>
            <? submit_button(); ?>
    </form>
</div>
是的,正如你可能知道的,这是另一个图像库插件。我做了一次搜索和替换,以隐藏我为谁做插件的一些信息。希望这样做不会带来任何错误。

2 个回复
最合适的回答,由SO网友:David Kees 整理而成

Chip Bennett在上述对话中确定的问题与settings_errors() 呼叫问题是,我没有指定要显示错误或更新通知的设置,这主要是因为我所学习的教程也没有指定。通过指定该设置,我可以看到一个通知,并在必要时看到错误。

谢谢Chip的帮助!

SO网友:Mashiur Rahman

我们可以使用其中的选项页slug轻松解决这个问题,比如,settings_errors(\'page_slug\');我不确定这是对还是错,但对我来说效果很好。

结束

相关推荐