php – How to validate all WordPress plugin setting fields that are under one section using only one register_setting function?-ThrowExceptions

Exception or error:

I am trying to validate setting fields that are under a single section and part of a single register_fuction. Here is the code

    <?php
/*
Plugin Name: Settings API example
Plugin URI: http://example.com/
Description: A complete and practical example of use of the Settings API. This plugin creates a new plugin administration page.
Author: WROX
Author URI: http://wrox.com
*/

// Add a menu for our option page
add_action('admin_menu', 'boj_myplugin_add_page');
function boj_myplugin_add_page() {
    add_options_page( 'My Plugin', 'My Plugin', 'manage_options', 'boj_myplugin', 'boj_myplugin_option_page' );
}

// Draw the option page
function boj_myplugin_option_page() {
    ?>
    <div class="wrap">
        <?php screen_icon(); ?>
        <h2>My plugin</h2>
        <form action="options.php" method="post">
            <?php settings_fields('boj_myplugin_options'); ?>
            <?php do_settings_sections('boj_myplugin'); ?>
            <input name="Submit" type="submit" value="Save Changes" />
        </form>
    </div>
    <?php
}

// Register and define the settings
add_action('admin_init', 'boj_myplugin_admin_init');
function boj_myplugin_admin_init(){
    register_setting(
        'boj_myplugin_options',
        'boj_myplugin_options',
        'boj_myplugin_validate_options'
    );
    add_settings_section(
        'boj_myplugin_main',
        'My Plugin Settings',
        'boj_myplugin_section_text',
        'boj_myplugin'
    );
    add_settings_field(
        'boj_myplugin_text_string',
        'Enter text here',
        'boj_myplugin_setting_input',
        'boj_myplugin',
        'boj_myplugin_main'
    );
   add_settings_field(
        'boj_myplugin_color_string',
        'Enter Color here',
        'boj_myplugin_color_input',
        'boj_myplugin',
        'boj_myplugin_main'
    );
}

// Draw the section header
function boj_myplugin_section_text() {
    echo '<p>Enter your settings here.</p>';
}

// Display and fill the form field
function boj_myplugin_setting_input() {
    // get option 'text_string' value from the database
    $options = get_option( 'boj_myplugin_options' );
    $text_string = $options['text_string'];
    // echo the field
    echo "<input id='text_string' name='boj_myplugin_options[text_string]' type='text' value='$text_string' />";
}

 function boj_myplugin_color_input() {
    // get option 'text_string' value from the database
    $options = get_option( 'boj_myplugin_options' );
    $color_string = $options['color_string'];
    // echo the field
    echo "<input id='color_string' name='boj_myplugin_options[color_string]' type='text' value='$color_string' />";
}

// Validate user input (we want text only)
function boj_myplugin_validate_options( $input ) {
    $valid['text_string'] = preg_replace( '/[^a-zA-Z]/', '', $input['text_string'] );
    $valid['color_string'] = preg_replace( '/|^#([A-Fa-f0-9]{3}){1,2}$|/', '', $input['color_string'] );

    if( $valid['text_string'] != $input['text_string'] ) {
        add_settings_error(
            'boj_myplugin_text_string',
            'boj_myplugin_texterror',
            'Incorrect value entered!',
            'error'
        );      
    } elseif( $valid['color_string'] != $input['color_string'] ) {
        add_settings_error(
            'boj_myplugin_text_string',
            'boj_myplugin_texterror',
            'Incorrect color entered!',
            'error'
        );      
    }

    return $valid;
}

The text validation works perfectly but the color validation doesn’t work. I tried to change regex still it doesn’t work. Can you tell me where is the error?
Should I use different register_setting functions for each field?

How to solve:

Leave a Reply

Your email address will not be published. Required fields are marked *