我正在征求关于如何编写此代码的最佳实践的建议。目前,我有简单的主题选项和文本字段,在我的模板中输出信息。我目前正在使用此代码进行设置api和纯文本清理。我的问题是,另一个设置字段是网站字段,也是电子邮件字段。我不确定是否必须创建另一个完整的主题选项、节和字段,以便可以直接单独清理已注册的设置(并对每种类型进行正确清理),或者是否可以在同一个主题中组合所有设置oem_theme_profile_options
卫生处理。我还不是最好的php高手。因此,从最佳实践的角度来理解这一点将有助于我对未来的教育,而不是让我在数据库中创建多个选项。
function oem_theme_initialize_profile_options() {
if( false == get_option(\'oem_theme_profile_options\')) {
add_option(\'oem_theme_profile_options\');
}
add_settings_section(
\'profile_settings_section\',
\'Profile Options\',
\'oem_profile_options_callback\',
\'oem_theme_profile_options\'
);
add_settings_field(
\'personal_name\',
\'Name\',
\'oem_personal_name_callback\',
\'oem_theme_profile_options\',
\'profile_settings_section\'
);
register_setting(
\'oem_theme_profile_options\',
\'oem_theme_profile_options\',
\'oem_theme_sanitize_profile_options\' // Here is where all these options get sanitized the same.
);
} // end of oem_theme_initialize_profile_options
add_action(\'admin_init\', \'oem_theme_initialize_profile_options\');
function oem_profile_options_callback() {
echo \'<p>Provide the URL to the profile networks you\\\'d like to display</p>\';
} // end oem_profile_options_callback
function oem_personal_name_callback() {
// First, we read the profile options collection
$options = get_option(\'oem_theme_profile_options\');
// Next, we need to make sure the elment is defined in the options. If not, we\'ll set an empty string.
$url = \'\';
if (isset( $options[\'personal_name\'] )) {
$url = $options[\'personal_name\'];
}
// Render the output
echo \'<input type="text" id="personal_name" name="oem_theme_profile_options[personal_name]" value="\' . $options[\'personal_name\'] . \'" />\';
} // end oem_personal_name_callback
文本清理function oem_theme_sanitize_profile_options($input) {
//Define the array for the updated options
$output = array();
// Loop through each of the options sanitizing the data
foreach ($input as $key => $val) {
if( isset($input[$key]) ) {
$output[$key] = strip_tags( stripslashes($input[$key]));
} // end if
} // end foreach
return apply_filters( \'oem_theme_sanitize_profile_options\', $output, $input );
} // end oem_theme_sanitize_profile_options