0

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

1

The setting that is the first parameter is the not the option key. It is the key that you would've registered using register_setting

register_setting( 'settings_unique_key4172_91h, 'option_key', 'name_of_sanitiser_function' );

Then you can add settings errors like this, inside the callback

function name_of_sanitiser_function(){

    add_settings_error(
        'settings_unique_key4172_91h',
        esc_attr( 'settings_updated' ),
        $message,
        $type
    );

}

You might like a few tutorials. I agree personally that it is very confusing:

http://www.presscoders.com/2010/05/wordpress-settings-api-explained/

http://ottopress.com/2009/wordpress-settings-api-tutorial/

  • so if i add new add_settings_error('my_err') i must register_settings() for all added settings error? – Ivan Mar 2 '15 at 13:27
  • No, the idea is that your 'setting' has a lot of fields that are stored together in one array. Your individual fields are handled via codex.wordpress.org/Function_Reference/add_settings_field For the rest you should read a tutorial. – Saurabh Shukla Mar 2 '15 at 13:32
  • I will first read mora in this tutorials what u post. Thanks – Ivan Mar 2 '15 at 13:38
  • I read all from this two link what u post. I dont found any example about using erorrs – Ivan Mar 2 '15 at 14:13
  • Those were for understanding Settings API in general. See my edit above for the short answer. The add_settings_error should be called inside the callback function. – Saurabh Shukla Mar 2 '15 at 14:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.