Created
October 9, 2012 14:51
-
-
Save gicolek/3859312 to your computer and use it in GitHub Desktop.
Faciilitate creation of settings page.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* This class facilitates creation of settings field to the theme options | |
* through, default pre-made boxes | |
* | |
* @todo example usage | |
* | |
* @author Rafal Gicgier <[email protected]> | |
*/ | |
class Skeleton_Settings { | |
private $options = array( ); | |
function __construct($args = array( )) { | |
$this->options = $args; | |
$args = array_merge( | |
array( | |
'config_file' => TEMPLATEPATH . DIRECTORY_SEPARATOR . 'skeleton/config.php', | |
), $args | |
); | |
add_action( 'admin_init', array( $this, 'admin_init' ) ); | |
add_action( 'admin_menu', array( $this, 'options_page' ) ); | |
} | |
/** | |
* Generates Backend Page for options | |
* | |
* @hook add_theme_page | |
*/ | |
public function do_options_page() { | |
?> | |
<div class="wrap"> | |
<?php | |
if ( isset( $_GET['settings-updated'] ) ) { | |
echo "<div class='updated'><p>Theme settings updated successfully.</p></div>"; | |
} | |
?> | |
<?php | |
// @todo http://codex.wordpress.org/Function_Reference/get_settings_errors | |
$errors = get_settings_errors(); | |
if ( $errors ) { | |
echo "<div class='error'>"; | |
foreach ( $errors as $error ) { | |
echo $error['message']; | |
} | |
echo "</div>"; | |
} | |
?> | |
<form action="options.php" method="post"> | |
<?php | |
settings_fields( 'skeleton_options' ); | |
do_settings_sections( 'skeleton_settings' ); | |
?> | |
<br /> | |
<input name="skeleton_options[submit]" type="submit" class="button-primary" value="<?php esc_attr_e( 'Save Settings', 'skeleton' ); ?>" /> | |
</form> | |
</div> | |
<?php | |
} | |
/** | |
* Utility that registers setting, adds setting section and adds field for each | |
* field provided through config array | |
* | |
* @hook admin_init | |
*/ | |
function admin_init() { | |
register_setting( 'skeleton_options', 'skeleton_options', array( $this, 'options_validate' ) ); | |
add_settings_section( 'skeleton_main', 'Main Settings', array( $this, 'options_text' ), 'skeleton_settings' ); | |
foreach ( $this->options as $option ) { | |
add_settings_field( $option['name'], $option['desc'], array( $this, 'options_generate_fields' ), 'skeleton_settings', 'skeleton_main', $option ); | |
} | |
} | |
/** | |
* Generate section's text | |
*/ | |
function options_text() { | |
echo '<p>Change the footer about text here.</p>'; | |
} | |
/** | |
* Generates fields for each option specified | |
* | |
* @param {array of strings} $option | |
*/ | |
function options_generate_fields($option) { | |
if ( $option['type'] == 'text' ) { | |
$this->generate_text_field( $option ); | |
} else if ( $option['type'] == 'dropdown_pages' ) { | |
$this->generate_dropdown_pages_field( $option ); | |
} else if ( $option['type'] == 'wp_editor' ) { | |
$this->generate_wp_editor( $option ); | |
} else { | |
// @todo throw an error? | |
} | |
} | |
/** | |
* Generates text field | |
* | |
* @param {array of strings} $option | |
*/ | |
function generate_text_field($option) { | |
$options = get_option( 'skeleton_options' ); | |
if ( !empty( $options[$option['name']] ) ) { | |
echo "<input id='{$option['name']}' name='skeleton_options[{$option['name']}]' size='80' type='text' value='{$options[$option['name']]}' />"; | |
} | |
else | |
echo "<input id='{$option['name']}' name='skeleton_options[{$option['name']}]' size='80' type='text' value='' />"; | |
} | |
/** | |
* Generates dropdown of all listed pages | |
* | |
* @param {array of strings} $option | |
*/ | |
function generate_dropdown_pages_field($option) { | |
$options = get_option( 'skeleton_options' ); | |
if ( !empty( $options[$option['name']] ) ) { | |
$args = array( | |
'selected' => $options[$option['name']], | |
'name' => "skeleton_options[{$option['name']}]", | |
); | |
wp_dropdown_pages( $args ); | |
} else { | |
$args = array( | |
'name' => "skeleton_options[{$option['name']}]", | |
); | |
wp_dropdown_pages( $args ); | |
} | |
} | |
/** | |
* Generates wp_editor textarea | |
* | |
* @param {array of strings} $option | |
*/ | |
function generate_wp_editor($option) { | |
$options = get_option( 'skeleton_options' ); | |
$settings = array( 'textarea_name' => "skeleton_options[{$option['name']}]", 'media_buttons' => false, 'wpautop' => true ); | |
if ( !empty( $options[$option['name']] ) ) { | |
wp_editor( $options[$option['name']], $option['name'], $settings ); | |
} | |
else | |
wp_editor( '', $option['name'], $settings ); | |
} | |
/** | |
* Validates the input | |
* | |
* @param {mixed} $input | |
* @return {mixed} | |
*/ | |
function options_validate($input) { | |
$valid = array( ); | |
foreach ( $this->options as $option ) { | |
if ( $option['type'] == 'text' ) | |
$valid[$option['name']] = sanitize_text_field( $input[$option['name']] ); | |
else if ( $option['type'] == 'dropdown_pages' ) { | |
$valid[$option['name']] = $input[$option['name']]; | |
} | |
else if ( $option['type'] == 'wp_editor' ) { | |
$valid[$option['name']] = $input[$option['name']]; | |
} | |
// @todo if | |
} | |
// add_settings_error( 'secreta_about', 'settings_updated', 'error fucker' ); | |
return $valid; | |
} | |
/** | |
* Adds theme options page | |
* | |
* @hook admin_menu | |
*/ | |
function options_page() { | |
add_theme_page( 'Theme Options', 'Theme Options', 'manage_options', 'skeleton_settings', array( $this, 'do_options_page' ) ); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment