Skip to content
This repository
tree: 5f44856a45
Fetching contributors…

Cannot retrieve contributors at this time

file 305 lines (275 sloc) 9.875 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305
<?php
/**
* _s Theme Options
*
* @package _s
* @since _s 1.0
*/

/**
* Register the form setting for our _s_options array.
*
* This function is attached to the admin_init action hook.
*
* This call to register_setting() registers a validation callback, _s_theme_options_validate(),
* which is used when the option is saved, to ensure that our option values are properly
* formatted, and safe.
*
* @since _s 1.0
*/
function _s_theme_options_init() {
register_setting(
'_s_options', // Options group, see settings_fields() call in _s_theme_options_render_page()
'_s_theme_options', // Database option, see _s_get_theme_options()
'_s_theme_options_validate' // The sanitization callback, see _s_theme_options_validate()
);

// Register our settings field group
add_settings_section(
'general', // Unique identifier for the settings section
'', // Section title (we don't want one)
'__return_false', // Section callback (we don't want anything)
'theme_options' // Menu slug, used to uniquely identify the page; see _s_theme_options_add_page()
);

// Register our individual settings fields
add_settings_field(
'sample_checkbox', // Unique identifier for the field for this section
__( 'Sample Checkbox', '_s' ), // Setting field label
'_s_settings_field_sample_checkbox', // Function that renders the settings field
'theme_options', // Menu slug, used to uniquely identify the page; see _s_theme_options_add_page()
'general' // Settings section. Same as the first argument in the add_settings_section() above
);

add_settings_field( 'sample_text_input', __( 'Sample Text Input', '_s' ), '_s_settings_field_sample_text_input', 'theme_options', 'general' );
add_settings_field( 'sample_select_options', __( 'Sample Select Options', '_s' ), '_s_settings_field_sample_select_options', 'theme_options', 'general' );
add_settings_field( 'sample_radio_buttons', __( 'Sample Radio Buttons', '_s' ), '_s_settings_field_sample_radio_buttons', 'theme_options', 'general' );
add_settings_field( 'sample_textarea', __( 'Sample Textarea', '_s' ), '_s_settings_field_sample_textarea', 'theme_options', 'general' );
}
add_action( 'admin_init', '_s_theme_options_init' );

/**
* Change the capability required to save the '_s_options' options group.
*
* @see _s_theme_options_init() First parameter to register_setting() is the name of the options group.
* @see _s_theme_options_add_page() The edit_theme_options capability is used for viewing the page.
*
* @param string $capability The capability used for the page, which is manage_options by default.
* @return string The capability to actually use.
*/
function _s_option_page_capability( $capability ) {
return 'edit_theme_options';
}
add_filter( 'option_page_capability__s_options', '_s_option_page_capability' );

/**
* Add our theme options page to the admin menu.
*
* This function is attached to the admin_menu action hook.
*
* @since _s 1.0
*/
function _s_theme_options_add_page() {
$theme_page = add_theme_page(
__( 'Theme Options', '_s' ), // Name of page
__( 'Theme Options', '_s' ), // Label in menu
'edit_theme_options', // Capability required
'theme_options', // Menu slug, used to uniquely identify the page
'_s_theme_options_render_page' // Function that renders the options page
);
}
add_action( 'admin_menu', '_s_theme_options_add_page' );

/**
* Returns an array of sample select options registered for _s.
*
* @since _s 1.0
*/
function _s_sample_select_options() {
$sample_select_options = array(
'0' => array(
'value' => '0',
'label' => __( 'Zero', '_s' )
),
'1' => array(
'value' => '1',
'label' => __( 'One', '_s' )
),
'2' => array(
'value' => '2',
'label' => __( 'Two', '_s' )
),
'3' => array(
'value' => '3',
'label' => __( 'Three', '_s' )
),
'4' => array(
'value' => '4',
'label' => __( 'Four', '_s' )
),
'5' => array(
'value' => '5',
'label' => __( 'Five', '_s' )
)
);

return apply_filters( '_s_sample_select_options', $sample_select_options );
}

/**
* Returns an array of sample radio options registered for _s.
*
* @since _s 1.0
*/
function _s_sample_radio_buttons() {
$sample_radio_buttons = array(
'yes' => array(
'value' => 'yes',
'label' => __( 'Yes', '_s' )
),
'no' => array(
'value' => 'no',
'label' => __( 'No', '_s' )
),
'maybe' => array(
'value' => 'maybe',
'label' => __( 'Maybe', '_s' )
)
);

return apply_filters( '_s_sample_radio_buttons', $sample_radio_buttons );
}

/**
* Returns the options array for _s.
*
* @since _s 1.0
*/
function _s_get_theme_options() {
$saved = (array) get_option( '_s_theme_options' );
$defaults = array(
'sample_checkbox' => 'off',
'sample_text_input' => '',
'sample_select_options' => '',
'sample_radio_buttons' => '',
'sample_textarea' => '',
);

$defaults = apply_filters( '_s_default_theme_options', $defaults );

$options = wp_parse_args( $saved, $defaults );
$options = array_intersect_key( $options, $defaults );

return $options;
}

/**
* Renders the sample checkbox setting field.
*/
function _s_settings_field_sample_checkbox() {
$options = _s_get_theme_options();
?>
<label for"sample-checkbox">
<input type="checkbox" name="_s_theme_options[sample_checkbox]" id="sample-checkbox" <?php checked( 'on', $options['sample_checkbox'] ); ?> />
<?php _e( 'A sample checkbox.', '_s' ); ?>
</label>
<?php
}

/**
* Renders the sample text input setting field.
*/
function _s_settings_field_sample_text_input() {
$options = _s_get_theme_options();
?>
<input type="text" name="_s_theme_options[sample_text_input]" id="sample-text-input" value="<?php echo esc_attr( $options['sample_text_input'] ); ?>" />
<label class="description" for="sample-text-input"><?php _e( 'Sample text input', '_s' ); ?></label>
<?php
}

/**
* Renders the sample select options setting field.
*/
function _s_settings_field_sample_select_options() {
$options = _s_get_theme_options();
?>
<select name="_s_theme_options[sample_select_options]" id="sample-select-options">
<?php
$selected = $options['sample_select_options'];
$p = '';
$r = '';

foreach ( _s_sample_select_options() as $option ) {
$label = $option['label'];
if ( $selected == $option['value'] ) // Make default first in list
$p = "\n\t<option style=\"padding-right: 10px;\" selected='selected' value='" . esc_attr( $option['value'] ) . "'>$label</option>";
else
$r .= "\n\t<option style=\"padding-right: 10px;\" value='" . esc_attr( $option['value'] ) . "'>$label</option>";
}
echo $p . $r;
?>
</select>
<label class="description" for="sample_theme_options[selectinput]"><?php _e( 'Sample select input', '_s' ); ?></label>
<?php
}

/**
* Renders the radio options setting field.
*
* @since _s 1.0
*/
function _s_settings_field_sample_radio_buttons() {
$options = _s_get_theme_options();

foreach ( _s_sample_radio_buttons() as $button ) {
?>
<div class="layout">
<label class="description">
<input type="radio" name="_s_theme_options[sample_radio_buttons]" value="<?php echo esc_attr( $button['value'] ); ?>" <?php checked( $options['sample_radio_buttons'], $button['value'] ); ?> />
<?php echo $button['label']; ?>
</label>
</div>
<?php
}
}

/**
* Renders the sample textarea setting field.
*/
function _s_settings_field_sample_textarea() {
$options = _s_get_theme_options();
?>
<textarea class="large-text" type="text" name="_s_theme_options[sample_textarea]" id="sample-textarea" cols="50" rows="10" /><?php echo esc_textarea( $options['sample_textarea'] ); ?></textarea>
<label class="description" for="sample-textarea"><?php _e( 'Sample textarea', '_s' ); ?></label>
<?php
}

/**
* Renders the Theme Options administration screen.
*
* @since _s 1.0
*/
function _s_theme_options_render_page() {
?>
<div class="wrap">
<?php screen_icon(); ?>
<h2><?php printf( __( '%s Theme Options', '_s' ), get_current_theme() ); ?></h2>
<?php settings_errors(); ?>

<form method="post" action="options.php">
<?php
settings_fields( '_s_options' );
do_settings_sections( 'theme_options' );
submit_button();
?>
</form>
</div>
<?php
}

/**
* Sanitize and validate form input. Accepts an array, return a sanitized array.
*
* @see _s_theme_options_init()
* @todo set up Reset Options action
*
* @param array $input Unknown values.
* @return array Sanitized theme options ready to be stored in the database.
*
* @since _s 1.0
*/
function _s_theme_options_validate( $input ) {
$output = array();

// Checkboxes will only be present if checked.
if ( isset( $input['sample_checkbox'] ) )
$output['sample_checkbox'] = 'on';

// The sample text input must be safe text with no HTML tags
if ( isset( $input['sample_text_input'] ) && ! empty( $input['sample_text_input'] ) )
$output['sample_text_input'] = wp_filter_nohtml_kses( $input['sample_text_input'] );

// The sample select option must actually be in the array of select options
if ( isset( $input['sample_select_options'] ) && array_key_exists( $input['sample_select_options'], _s_sample_select_options() ) )
$output['sample_select_options'] = $input['sample_select_options'];

// The sample radio button value must be in our array of radio button values
if ( isset( $input['sample_radio_buttons'] ) && array_key_exists( $input['sample_radio_buttons'], _s_sample_radio_buttons() ) )
$output['sample_radio_buttons'] = $input['sample_radio_buttons'];

// The sample textarea must be safe text with the allowed tags for posts
if ( isset( $input['sample_textarea'] ) && ! empty( $input['sample_textarea'] ) )
$output['sample_textarea'] = wp_filter_post_kses( $input['sample_textarea'] );

return apply_filters( '_s_theme_options_validate', $output, $input );
}
Something went wrong with that request. Please try again.