0

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
}
0

If you are use add_menu_page() or add_submenu_page() to add setting pages,Try add this function in your code:

settings_errors();
  • I currently added that – Farhad Sakhaei Aug 17 '17 at 19:20
  • is it ok?if not,do you mind past the complete code? – Amos Lee Aug 17 '17 at 21:50
  • I added another function too – Farhad Sakhaei Aug 18 '17 at 6:15
0

No need to add settings_errors beside the form
WordPress do that when need
So remove the line :

settings_errors( 'persianfont_messages' );

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.