Am not shure how to realy use settings api for showing error messages on my menu-page. For example i create menu page (eng.product)and there i register two input text fields like example. After submiting my form in my custom page error message is the some in all options page "reading, writing, premalinks, ...etc". Whay is not only on that page what i create.
Here is code how i use error funcition. Maybe this is wrong way how i use it, for that i post this question:
/**
* Init Admin Options
*
*/
public function initAdminOptions()
{
// Init settings options
$this->createSettingSection();
$this->createOptionFields();
$this->registerSettingsOptions();
$this->options = get_option('isl_ecomerce');
if(isset($_POST['submit'])) {
$this->options = array(
'product_name' => sanitize_text_field($_POST['product_name']),
'product_price' => sanitize_text_field($_POST['product_price']),
'product_desc' => esc_textarea($_POST['product_desc'])
);
// Validate fields
if($this->options['product_name'] == '' || $this->options['product_price'] == '') {
add_settings_error('product_message', '', 'All field required!', 'error');
return;
}else {
// Check if table exist
if( false === get_option('isl_ecomerce')) {
add_option('isl_ecomerce', $this->options);
add_settings_error('product_message', '', 'Settings Saved...', 'updated');
}else {
// Already exist? Update it
update_option('isl_ecomerce', $this->options);
add_settings_error('isl_ecomerce', '', 'Settings Updated..', 'updated');
}
}
}
}
And i register hook for settings_error
/**
* Settings Errors
*
*/
public function option_notices()
{
settings_errors('product_message');
}
In my __construct i do :
add_action('admin_notices', array(&$this, 'option_notices'));
And options page
/**
* Create Options Page
*
*/
public function createOptionsPage()
{
add_menu_page(
__('Products', 'domain'),
__('Products', 'domain'),
'manage_options',
'products_page',
array(&$this, 'optionsPageCallback')
);
}
After this i have the some message "All field required!", "Settings Saved...", "settings updated..." on all pages in wp-admin. How to show only in my page