I’ve used the settings API to create a very simple page for plugin settings. This is my first time using the API, and through various tutorials online and answers here, I think I’ve figured it out. This is backed up by the fact that the page even works!!
However, something weird is happening:
As you can see above, there are two div.update messages being displayed on the screen. I’ve checked and there’s only one call to the settings_errors()
function so and that function isn’t called in a loop or anything that I can find, but I’m still getting two notices.
Here’s the code:
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);
}
The following is the contents of the plugin-settings.php
page that’s included in the code above:
<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>
And, yes, as you can probably tell, this is Yet Another Image Gallery Plugin. I did do a search and replace to hide some of the information about for whom I’m doing the plugin. Hopefully doing so didn’t introduce any errors.
The issue, as determined by Chip Bennett in our conversation above, was related to the
settings_errors()
call. The problem was that I didn’t specify a setting for which I wanted to display either errors or update notices mostly because the tutorial from which I was working didn’t either. By specifying that setting, I see one single notice and the errors if necessary.Thanks, Chip, for the help!
We can easily solve this by using the options page slug in it, like,
settings_errors('page_slug');
I’m not sure is it right or wrong way but worked for me well.