开发者

Input validation displays error message that should only show up in an if statement

I am putting together a WordPress theme options page. I am trying to figure out if a url entered into a text field passes a validation. If it doesn't, I want to display a message at the top of the admin page to correct what is entered.

In this case, I want to display File type must have the file extension .jpg, .jpeg, .gif or .png at the top of the page when a user enters a file with any other extension. This message is within an if statement, but it is showing up regardless of what is typed into the field. I would like to know what mistake I am making here or if the input is even being validated.

Here is the code within the initialization of the options page

add_action('admin_init', 'theme_admin_init');

function theme_admin_init() {
    register_setting(
        'coolorange_theme_options',
        'coolorange_options',
        'coolorange_options_validate'
    );
    // what each parameter represents:
    // add_settings_field($id, $title, $callback, $page, $section, $args);
    add_settings_section(
        'coolorange_logo_main',
        'Logo Section Settings',
        'logo_section_text',
        'coolorange'
    );
    add_settings_field(
        'upload_image_button',
        '<strong>Upload logo to the Media Folder</strong>',
        'file_upload_button',
        'coolorange',
        'coolorange_logo_main'
    ); // Upload Logo button
    add_settings_field(
        'logo_textfields',
        '<strong>Logo location</strong>',
        'file_location',
        'coolorange',
        'coolorange_logo_main'
    ); // logo url, width and height text fields
    add_settings_field(
        'restore_selectbox',
        '<strong>Restore original heading</strong>',
        'restore_dropdown',
        'coolorange',
        'coolorange_logo_main'
    );
}

Here is the code for the input box (this is within a file_location() function):

<strong>File URL:</strong> 开发者_StackOverflow社区<input id="image_url" type="text" value="<?php $options['image_url']; ?>" size="60" name="coolorange_options[image_url]" />

And this is the validation code:

//Validation
function coolorange_options_validate($input) {
    $options = get_option('coolorange_theme_options');
    //check filetypes for image url
    $options['image_url'] = trim($input['image_url']);
    if ( !preg_match ( '/\.(gif|jpg|jpeg|png)$/', $options['image_url'] ) ) { //opens if statement
        $options['image_url'] = ''; 
        echo '<div id="message" style="color: red;"><p>File type must have the file extension .jpg, .jpeg, .gif or .png</p></div>';
    } // closes if statement
    else {

    }
    return $options;
}

add_action('admin_notices', 'coolorange_options_validate'); 
//shows validation errors at the top of the page


Looks like problem is in the html name in following line

<strong>File URL:</strong> <input id="image_url" type="text" value="<?php $options['image_url']; ?>" size="60" name="coolorange_options[image_url]" />

It should be like following

<strong>File URL:</strong> <input id="image_url" type="text" value="<?php $options['image_url']; ?>" size="60" name="coolorange_theme_options[image_url]" />

As you are expecting $options = get_option('coolorange_theme_options');.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜