Skip to content

Instantly share code, notes, and snippets.

@tlovett1
Created March 28, 2014 16:37
Show Gist options
  • Save tlovett1/9837157 to your computer and use it in GitHub Desktop.
Save tlovett1/9837157 to your computer and use it in GitHub Desktop.
<?php
class NYO_General_Settings {
private static $_instance;
private $option_defaults = array(
'trending_title' => array(
'sanitizer' => 'sanitize_text_field',
'default' => 'Trending:',
)
);
private function __construct() {
add_action( 'admin_menu', array( $this, 'action_admin_menu' ) );
add_action( 'admin_init', array( $this, 'action_admin_init' ) );
}
public static function get_instance() {
if ( empty( self::$_instance ) ) {
self::$_instance = new NYO_General_Settings();
}
return self::$_instance;
}
/**
* Add options page
*
* @return void
*/
public function action_admin_menu() {
add_submenu_page( 'themes.php', 'Theme Settings', 'Theme Settings', 'manage_options', 'nyo-theme-settings', array( $this, 'screen_options' ) );
}
/**
* Register setting and sanitization callback
*
* @return void
*/
public function action_admin_init() {
register_setting( 'nyo_general_settings', 'nyo_general_settings', array( $this, 'sanitize_options' ) );
}
/**
* Sanitize options
*
* @param array $options
* @return array
*/
public function sanitize_options( $options ) {
$new_options = array();
foreach ( $this->option_defaults as $option_name => $option_array ) {
if ( ! isset( $options[$option_name] ) ) {
$value = $option_array['default'];
} else {
$value = $options[$option_name];
}
$new_options[$option_name] = call_user_func( $option_array['sanitizer'], $value );
}
return $new_options;
}
/**
* Output settings
*
* @return void
*/
public function screen_options() {
$option = get_option( 'nyo_general_settings' );
?>
<div class="wrap">
<h2>Observer Theme Settings</h2>
<form action="options.php" method="post">
<?php settings_fields( 'nyo_general_settings' ); ?>
<table class="form-table">
<tbody>
<tr>
<th scope="row"><label for="nyo_trending_title">Trending Title:</label></th>
<td>
<input type="text" id="nyo_trending_title" value="<?php echo esc_attr( $option['trending_title'] ); ?>" name="nyo_general_settings[trending_title]" />
</td>
</tr>
</tbody>
</table>
<?php submit_button(); ?>
</form>
</div>
<?php
}
}
NYO_General_Settings::get_instance();
/**
* Output escaped trending title
*/
function nyo_get_trending_title() {
$option = get_option( 'nyo_general_settings' );
if ( ! empty( $option['trending_title'] ) ) {
return esc_html( $option['trending_title'] );
}
return '';
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment