• I am developing a plugin which uses options API.

    I defined a class that uses a function to sanitize the inputs, but when I submit the options form, It displays errors and info twice.

    And also I didn’t succeed at displaying the error messages.

    What is the proper way to display the successful and error messages on validation of options form?

    Thank you.

    register_setting(
        $this->plugin_slug, // option_group
        $this->plugin_slug, // option_name, for name property of tags
        [$this, 'process_inputs'] // sanitize_callback
    );
    
    public function process_inputs( $input ){
        // sanitize functions:
        // sanitize_email(), sanitize_file_name(), sanitize_html_class(), sanitize_key(), sanitize_meta(), sanitize_mime_type(),
        // sanitize_option(), sanitize_sql_orderby(), sanitize_text_field(), sanitize_textarea_field(), sanitize_title(),
        // sanitize_title_for_query(), sanitize_title_with_dashes(), sanitize_user()
        $options = [];
        if( isset( $input['frontend-font'] ) and $input['frontend-font'] == true ) {
            $options['frontend-font'] = true;
        }else{
            $options['frontend-font'] = false;
        }
        if( isset( $input['backend-font'] ) and $input['backend-font'] == true ){
            $options['backend-font'] = true;
        }else{
            $options['backend-font'] = false;
        }
    
        // add error/update messages
        // check if the user have submitted the settings
        // wordpress will add the "settings-updated" $_GET parameter to the url
        if ( isset( $_GET['settings-updated'] ) ) {
            // add settings saved message with the class of "updated"*/
            add_settings_error(
                'persianfont_messages', // Slug title of setting
                'wporg_message', // Slug-name , Used as part of 'id' attribute in HTML output.
                __( 'settings saved.', 'persianfont' ), // message text, will be shown inside styled <div> and <p> tags
                'error' // Message type, controls HTML class. Accepts 'error' or 'updated'.
            );
        }
    
        return $options;
    }
        /**
         * Settings page display callback.
         */
        function settings_page_content() {
    		// check user capabilities
    		if ( ! current_user_can( 'manage_options' ) ) { return; }
    		
    		//var_dump( wp_load_alloptions() ); // print all options
    
    		// show error/update messages
    		settings_errors( 'persianfont_messages' );
    		?>
    		<div class="wrap">
    			<h1 class="wp-heading-inline"><?php echo esc_html($this->page_title); ?></h1>
    			<form method="post" action="options.php">
    			<?php
    				submit_button();
    				settings_fields( $this->plugin_slug ); // This prints out all hidden setting fields
    				do_settings_sections( $this->plugin_slug );
    				submit_button();
    			?>
    			</form>
    		</div>
    		<?php
        }
    
    • This topic was modified 6 years, 8 months ago by ParsMizban.
Viewing 1 replies (of 1 total)
  • Moderator bcworkz

    (@bcworkz)

    You need to tell register_setting() what argument you are passing.

    register_setting(
        $this->plugin_slug, // option_group
        $this->plugin_slug, // option_name, for name property of tags
        ['sanitize_callback'=>[$this, 'process_inputs'], // sanitize_callback
        ]
    );

    There are all sorts of arguments you can pass in the third $args parameter.

Viewing 1 replies (of 1 total)
  • The topic ‘add_settings_error on validating plugin options API’ is closed to new replies.