Created
June 19, 2014 07:26
-
-
Save Robbertdk/b3c09a4d3a694d3732b9 to your computer and use it in GitHub Desktop.
Wordpress theme option admin page. Works on CMB. Has hook for adding options, based on https://github.com/WebDevStudios/Custom-Metaboxes-and-Fields-for-WordPress/wiki/Using-CMB-to-create-an-Admin-Theme-Options-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 | |
/** | |
* CMB Theme Options | |
* @version 0.1.0 | |
*/ | |
class Theme_options_admin { | |
/** | |
* Option key, and option page slug | |
* @var string | |
*/ | |
protected static $key = 'theme_options'; | |
/** | |
* Array of metaboxes/fields | |
* @var array | |
*/ | |
protected static $options = array(); | |
/** | |
* Options Page title | |
* @var string | |
*/ | |
protected $title = ''; | |
/** | |
* Constructor | |
* @since 0.1.0 | |
*/ | |
public function __construct() { | |
// Set our title | |
$this->title = __( 'Site Options', 'goodbite' ); | |
} | |
/** | |
* Initiate our hooks | |
* @since 0.1.0 | |
*/ | |
public function hooks() { | |
add_action( 'admin_init', array( $this, 'init' ) ); | |
add_action( 'admin_menu', array( $this, 'add_options_page' ) ); | |
} | |
/** | |
* Register our setting to WP | |
* @since 0.1.0 | |
*/ | |
public function init() { | |
register_setting( self::$key, self::$key ); | |
} | |
/** | |
* Add menu options page | |
* @since 0.1.0 | |
*/ | |
public function add_options_page() { | |
$this->options_page = add_menu_page( $this->title, $this->title, 'manage_options', self::$key, array( $this, 'admin_page_display' ), '', 62 ); | |
} | |
/** | |
* Admin page markup. Mostly handled by CMB | |
* @since 0.1.0 | |
*/ | |
public function admin_page_display() { | |
?> | |
<div class="wrap cmb_options_page <?php echo self::$key; ?>"> | |
<h2><?php echo esc_html( get_admin_page_title() ); ?></h2> | |
<?php cmb_metabox_form( self::options_fields(), self::$key ); ?> | |
</div> | |
<?php | |
} | |
/** | |
* Adds fields to the option page fields | |
* @since 0.1.0 | |
* @param mulitdimensional arrays | |
* @return mulitdimensional arrays | |
*/ | |
public static function add_fields($fields){ | |
self::$options = self::options_fields(); | |
foreach ($fields as $field) { | |
if ( is_array($field) ){ | |
array_push(self::$options['fields'], $field); | |
} | |
} | |
} | |
/** | |
* Defines the theme option metabox and field configuration | |
* @since 0.1.0 | |
* @return array | |
*/ | |
public static function options_fields() { | |
// Only need to initiate the array once per page-load | |
if ( ! empty( self::$options ) ) | |
return self::$options; | |
self::$options = array( | |
'id' => 'options', | |
'show_on' => array( 'key' => 'options-page', 'value' => array( self::$key, ), ), | |
'show_names' => true, | |
'fields' => array(), | |
); | |
return self::$options; | |
} | |
/** | |
* Make public the protected $key variable. | |
* @since 0.1.0 | |
* @return string Option key | |
*/ | |
public static function key() { | |
return self::$key; | |
} | |
} | |
// Get it started | |
$goodbite_admin = new Theme_options_admin(); | |
$goodbite_admin->hooks(); | |
/** | |
* Wrapper function around cmb_get_option | |
* @since 0.1.0 | |
* @param string $key Options array key | |
* @return mixed Option value | |
*/ | |
function goodbite_get_option( $key = '' ) { | |
return cmb_get_option( Theme_options_admin::key(), $key ); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment