I am asking for advice on best practices on how to write this code. Currently I have simple theme options with text fields, outputting the info in my template. I am currently using this code for the settings api and sanitization of the plain text. My questions is, an additional settings field is a website field, and also an email field. I am not sure if I have to create another entire theme option, section and field so I can directly sanitize the registered settings individually (and properly for each type), or if this can all be combined within the same oem_theme_profile_options
sanitization. I am not the best php guy, yet. So figuring this out in terms of best practice will help educate me for the future and not lead me to create multiple options in the database.
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
Sanitization of text
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