我在放置WordPress主题选项页面。我想弄清楚输入到文本字段的URL是否通过了验证。如果没有,我想在管理页面的顶部显示一条消息来更正输入内容。输入验证显示错误消息,应该只显示在if语句中
在这种情况下,我想显示文件类型必须具有文件扩展名为.jpg,.JPEG,gif或在页面的顶部png格式当用户输入与任何其他扩展名的文件。此消息位于if语句内,但无论输入什么字段,它都会显示出来。我想知道我在这里做了什么错误,或者输入是否被验证。
这里是选项页
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'
);
}
这里的初始化中的代码是为输入框的代码(这是一个file_location()函数中):
<strong>File URL:</strong> <input id="image_url" type="text" value="<?php $options['image_url']; ?>" size="60" name="coolorange_options[image_url]" />
这是验证码:
//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
'$ options ['image_url']'的输出示例是什么?另外,'else'语句是否被执行? – 2011-06-05 06:31:48
由于我是PHP新手,我不确定如何回答这个问题,但示例输出将只是一个带有.jpg,.gif或.png文件扩展名的url。就else语句而言,我试图让函数不做任何事情,并保持原样。但是,这个else语句在这里可能是无关紧要的。 – 2011-06-05 17:21:27