<?php
/*
* Theme Options
*/
/*
* Register the form setting for our sdt_options array.
*
* This function is attached to the admin_init action hook.
*
* This call to register_setting() registers a validation callback, sdt_theme_options_validate(),
* which is used when the option is saved, to ensure that our option values are complete, properly
* formatted, and safe.
*
* We also use this function to add our theme option if it doesn't already exist.
*/
function sdt_theme_options_init
() {
// If we have no options in the database, let's add them now.
if (false === sdt_get_theme_options
())
add_option
('sdt_theme_options', sdt_get_default_theme_options
());
register_setting
(
'sdt_options', // Options group, see settings_fields() call in theme_options_render_page()
'sdt_theme_options', // Database option, see sdt_get_theme_options()
'sdt_theme_options_validate' // The sanitization callback, see sdt_theme_options_validate()
);
}
add_action
('admin_init', 'sdt_theme_options_init');
/*
* Change the capability required to save the 'sdt_options' options group.
*
* @see sdt_theme_options_init() First parameter to register_setting() is the name of the options group.
* @see sdt_theme_options_add_page() The edit_theme_options capability is used for viewing the page.
*
* By default, the options groups for all registered settings require the manage_options capability.
* This filter is required to change our theme options page to edit_theme_options instead.
* By default, only administrators have either of these capabilities, but the desire here is
* to allow for finer-grained control for roles and users.
*
* @param string $capability The capability used for the page, which is manage_options by default.
* @return string The capability to actually use.
*/
function sdt_option_page_capability
($capability) {
return 'edit_theme_options';
}
add_filter
('option_page_capability_sdt_options', 'sdt_option_page_capability');
/*
* Add our theme options page to the admin menu, including some help documentation.
*
* This function is attached to the admin_menu action hook.
*/
function sdt_theme_options_add_page
() {
$theme_page = add_theme_page
(
__
('Theme Options', 'sdt'), // Name of page
__
('Theme Options', 'sdt'), // Label in menu
'edit_theme_options', // Capability required
'theme_options', // Menu slug, used to uniquely identify the page
'sdt_theme_options_render_page' // Function that renders the options page
);
if (!$theme_page)
return;
$help = '<p>'.__
('Some themes provide customization options that are grouped together on a Theme Options screen. If you change themes, options may change or disappear, as they are theme-specific. Your current theme, Simple Dark Theme, provides the following Theme Options:', 'sdt').'</p>' .
'<ol>' .
'<li>'.__
( '<strong>Zenphoto integration</strong>: You can insert the URL of your Zenphoto installation (if on the same domain of this WordPress installation) to integrate it on a WordPress page.', 'sdt').'</li>'.
'</ol>'.
'<p>'.__
('Remember to click "Save Changes" to save any changes you have made to the theme options.', 'sdt').'</p>'.
'<p><strong>'.__
('For more information:', 'sdt').'</strong></p>'.
'<p>'.__
('<a href="http://codex.wordpress.org/Appearance_Theme_Options_Screen" target="_blank">Documentation on Theme Options</a>', 'sdt').'</p>'.
'<p>'.__
('<a href="http://wordpress.org/support/" target="_blank">Support Forums</a>', 'sdt').'</p>';
add_contextual_help
($theme_page, $help);
}
add_action
('admin_menu', 'sdt_theme_options_add_page');
/*
* Returns the default options
*/
function sdt_get_default_theme_options
() {
$default_theme_options = array(
'zenphoto_url' => 'http://'
);
}
/*
* Returns the options array
*/
function sdt_get_theme_options
() {
return get_option
('sdt_theme_options', sdt_get_default_theme_options
());
}
/*
* Returns the options page
*/
function sdt_theme_options_render_page
() {
?>
<div class="wrap">
<?php screen_icon
(); ?>
<h2>
<?php printf(__
('%s Theme Options', 'sdt'), get_current_theme
()); ?></h2>
<?php settings_errors
(); ?>
<form method="post" action="options.php" style="margin-top: 20px;">
<?php
settings_fields
('sdt_options');
$options = sdt_get_theme_options
();
$default_options = sdt_get_default_theme_options
();
?>
<fieldset>
<label>
<?php _e
('Zenphoto URL:', 'sdt'); ?></label>
<legend class="screen-reader-text"><span>
<?php _e
('Zenphoto URL', 'sdt'); ?></span></legend>
<input type="text" name="sdt_theme_options[zenphoto_url]" id="zenphoto-url" value="
<?php echo esc_attr
( $options['zenphoto_url'] ); ?>" size="60" />
</fieldset>
<?php submit_button
(); ?>
</form>
</div>
<?php
}
/*
* Sanitize and validate form input. Accepts an array, return a sanitized array.
*/
function sdt_theme_options_validate
($input) {
$output = $defaults = sdt_get_default_theme_options
();
// Zenphoto URL must be in our array
if (isset( $input['zenphoto_url']))
$output['zenphoto_url'] = $input['zenphoto_url'];
return apply_filters
('sdt_theme_options_validate', $output, $input, $defaults);
}
?>