add_menu( array( 'parent' => false, // parent ID or use 'false' for a root menu 'id' => 'asteroid_admin_bar', // link ID, defaults to a sanitized title value 'title' => ('Asteroid Options'), // link title 'href' => admin_url( 'themes.php?page=asteroid-options') )); $wp_admin_bar->add_menu( array( 'parent' => 'appearance', 'id' => 'plugins_admin_bar', 'title' => ('Plugins'), 'href' => admin_url( 'plugins.php') )); } add_action( 'wp_before_admin_bar_render', 'asteroid_admin_bar_render' ); /** * Theme Options Class * * Derived from the "WordPress Settings API" by Alison Barrett * http://alisothegeek.com */ class My_Theme_Options { private $sections; private $checkboxes; private $settings; /** * Construct * * @since 1.0 */ public function __construct() { // This will keep track of the checkbox options for the validate_settings function. $this->checkboxes = array(); $this->settings = array(); $this->get_option(); $this->sections['general'] = ( 'General' ); $this->sections['appearance'] = ( 'Appearance' ); $this->sections['custom-css'] = ( 'Custom CSS' ); $this->sections['widget-hooks'] = ( 'Widget Hooks' ); $this->sections['misc'] = ( 'Misc' ); $this->sections['reset'] = ( 'Reset Theme' ); add_action( 'admin_menu', array( &$this, 'add_pages' ) ); add_action( 'admin_init', array( &$this, 'register_settings' ) ); if ( ! get_option( 'asteroid_options' ) ) $this->initialize_settings(); } /* Add page(s) to the admin menu */ public function add_pages() { $admin_menu = add_theme_page( 'Asteroid Options', 'Asteroid Options', 'manage_options', 'asteroid-options', array( &$this, 'display_page' ) ); add_action( 'admin_print_scripts-' . $admin_menu, array( &$this, 'scripts' ) ); add_action( 'admin_print_styles-' . $admin_menu, array( &$this, 'styles' ) ); } /* Create settings field */ public function create_setting( $args = array() ) { $defaults = array( 'id' => 'default_field', 'title' => ( 'Default Field' ), 'desc' => ( 'This is a default description.' ), 'std' => '', 'type' => 'text', 'section' => 'general', 'choices' => array(), 'class' => '' ); extract( wp_parse_args( $args, $defaults ) ); $field_args = array( 'type' => $type, 'id' => $id, 'desc' => $desc, 'std' => $std, 'choices' => $choices, 'label_for' => $id, 'class' => $class ); if ( $type == 'checkbox' ) $this->checkboxes[] = $id; add_settings_field( $id, $title, array( $this, 'display_setting' ), 'asteroid-options', $section, $field_args ); } /* HTML to display the theme options page */ public function display_page() { echo '

' . ( 'Asteroid Options' ) . '

'; if ( isset( $_GET['settings-updated'] ) && $_GET['settings-updated'] == true ) echo '

' . ( 'Theme options updated.' ) . '

'; echo '
'; settings_fields( 'asteroid_options' ); echo '
    '; foreach ( $this->sections as $section_slug => $section ) echo '
  • ' . $section . '
  • '; echo '

'; do_settings_sections( $_GET['page'] ); echo '
'; echo ' '; echo '
'; } /** * HTML output for text field * * @since 1.0 */ public function display_setting( $args = array() ) { extract( $args ); $options = get_option( 'asteroid_options' ); if ( ! isset( $options[$id] ) && $type != 'checkbox' ) $options[$id] = $std; elseif ( ! isset( $options[$id] ) ) $options[$id] = 0; $field_class = ''; if ( $class != '' ) $field_class = ' ' . $class; switch ( $type ) { case 'heading': echo '

' . $desc . '

'; break; case 'checkbox': echo ' '; break; case 'select': echo ''; if ( $desc != '' ) echo '
' . $desc . ''; break; case 'radio': $i = 0; foreach ( $choices as $value => $label ) { echo ' '; if ( $i < count( $options ) - 1 ) echo '
'; $i++; } if ( $desc != '' ) echo '' . $desc . ''; break; case 'textarea': echo ''; if ( $desc != '' ) echo '
' . $desc . ''; break; case 'textarea-css': echo ''; if ( $desc != '' ) echo '
' . $desc . ''; break; case 'password': echo ''; if ( $desc != '' ) echo '
' . $desc . ''; break; case 'text': default: echo ''; if ( $desc != '' ) echo '
' . $desc . ''; break; case 'text-int': default: echo ''; if ( $desc != '' ) echo ' ' . $desc . ''; break; case 'upload': default: echo ' '; if ( $desc != '' ) echo '
' . $desc . ''; break; case 'color': default: echo ''; if ( $desc != '' ) echo '' . $desc . ''; break; } } /* Define all settings and their defaults */ public function get_option() { /* General Settings ===========================================*/ $this->settings['head_codes'] = array( 'title' => ( 'Custom <Head> Codes' ), 'desc' => ( 'Insert <Head> codes here.    e.g. Google Analytics, Metas, Fonts, Scripts and what not.' ), 'std' => '', 'type' => 'textarea', 'section' => 'general' ); $this->settings['menu_search'] = array( 'section' => 'general', 'title' => ( 'Search Box on Menu' ), 'desc' => ( 'Display a Search box on the Main Menu.' ), 'type' => 'checkbox', 'std' => 1 // Set to 1 to be checked by default, 0 to be unchecked by default. ); $this->settings['post_display_type'] = array( 'section' => 'general', 'title' => ( 'Home Post Display' ), 'desc' => ( 'Show excerpts or full posts on the home page.' ), 'type' => 'radio', 'std' => 'choice1', 'choices' => array( 'choice1' => 'Excerpts', 'choice2' => 'Full Post' ) ); $this->settings['loop_date_on'] = array( 'section' => 'general', 'title' => ( 'Post Date on Loops' ), 'desc' => ( 'Show the Post’s Date before the Title on Home, Searches, Categories and Archives.' ), 'type' => 'checkbox', 'std' => 1 ); $this->settings['single_date_on'] = array( 'section' => 'general', 'title' => ( 'Post Date on Singles' ), 'desc' => ( 'Show the Post’s Date before the Title on Single Posts.' ), 'type' => 'checkbox', 'std' => 1 ); $this->settings['full_post_date'] = array( 'section' => 'general', 'title' => ( 'Show Full Post Date' ), 'desc' => ( 'Show Full Post Date on Single Posts.' ), 'type' => 'checkbox', 'std' => 0 ); $this->settings['show_post_author'] = array( 'section' => 'general', 'title' => ( 'Show Post Author' ), 'desc' => ( 'Show the Post’s Author on Single Posts.' ), 'type' => 'checkbox', 'std' => 1 ); /* Appearance ===========================================*/ $this->settings['header_logo'] = array( 'section' => 'appearance', 'title' => ( 'Header Logo' ), 'desc' => ( 'The URL of your logo. This replaces the site Title & Description.' ), 'type' => 'upload', 'std' => '' ); $this->settings['favicon'] = array( 'section' => 'appearance', 'title' => ( 'Favicon' ), 'desc' => ( 'The URL of your favicon. It should be 16x16 pixels.' ), 'type' => 'upload', 'std' => '' ); $this->settings['header_height'] = array( 'title' => ( 'Header Height' ), 'desc' => ( 'px. Set the height of the Header.' ), 'std' => '120', 'type' => 'text-int', 'section' => 'appearance' ); $this->settings['main_width'] = array( 'title' => ( 'Main Width' ), 'desc' => ( 'px. Set the width of the main content/post area.' ), 'std' => '620', 'type' => 'text-int', 'section' => 'appearance' ); $this->settings['sidebar_width'] = array( 'title' => ( 'Sidebar Width' ), 'desc' => ( 'px. Set the width of the Sidebar.' ), 'std' => '310', 'type' => 'text-int', 'section' => 'appearance' ); $this->settings['header_bgcolor'] = array( 'title' => ( '#Header Color' ), 'desc' => ( 'Choose a background color for the #header container.' ), 'std' => 'FFFFFF', 'type' => 'color', 'section' => 'appearance' ); $this->settings['main_bgcolor'] = array( 'title' => ( '#Main Color' ), 'desc' => ( 'Choose a background color for the #main container.' ), 'std' => 'FFFFFF', 'type' => 'color', 'section' => 'appearance' ); $this->settings['sidebar_bgcolor'] = array( 'title' => ( '#Sidebar Color' ), 'desc' => ( 'Choose a background color for the #sidebar container.' ), 'std' => 'FFFFFF', 'type' => 'color', 'section' => 'appearance' ); /* Custom CSS ===========================================*/ $this->settings['custom_css'] = array( 'title' => ( 'Custom CSS Codes' ), 'desc' => ( 'Enter custom CSS here to apply to the theme. This should override any other stylings.' ), 'std' => '', 'type' => 'textarea-css', 'section' => 'custom-css', 'class' => 'textarea-css' ); /* Widget Hooks ===========================================*/ $this->settings['widget_hook_body'] = array( 'section' => 'widget-hooks', 'title' => ( 'Body' ), 'desc' => ( '' ), 'type' => 'checkbox', 'std' => 0 ); $this->settings['widget_hook_header'] = array( 'section' => 'widget-hooks', 'title' => ( 'Header' ), 'desc' => ( '' ), 'type' => 'checkbox', 'std' => 0 ); $this->settings['widget_hook_after_menu'] = array( 'section' => 'widget-hooks', 'title' => ( 'After Menu' ), 'desc' => ( '' ), 'type' => 'checkbox', 'std' => 0 ); $this->settings['widget_hook_before_post'] = array( 'section' => 'widget-hooks', 'title' => ( 'Before Post' ), 'desc' => ( '' ), 'type' => 'checkbox', 'std' => 0 ); $this->settings['widget_hook_before_post_content'] = array( 'section' => 'widget-hooks', 'title' => ( 'Before Post - Content' ), 'desc' => ( '' ), 'type' => 'checkbox', 'std' => 0 ); $this->settings['widget_hook_after_post_content'] = array( 'section' => 'widget-hooks', 'title' => ( 'After Post - Content' ), 'desc' => ( '' ), 'type' => 'checkbox', 'std' => 0 ); $this->settings['widget_hook_after_post'] = array( 'section' => 'widget-hooks', 'title' => ( 'After Post' ), 'desc' => ( '' ), 'type' => 'checkbox', 'std' => 0 ); /* Miscellaneous ===========================================*/ $this->settings['remove_wp_version'] = array( 'section' => 'misc', 'title' => ( 'Remove Wordpress Version' ), 'desc' => ( 'Prevent WP Version from being displayed in the <Head>' ), 'type' => 'checkbox', 'std' => 0 ); /* Hooks */ $this->settings['hook_body'] = array( 'title' => ( '#Body' ), 'desc' => ( 'Enter your scripts and html codes' ), 'std' => '', 'type' => 'textarea', 'section' => 'misc' ); $this->settings['hook_container'] = array( 'title' => ( '#Container' ), 'desc' => ( 'Enter your scripts and html codes' ), 'std' => '', 'type' => 'textarea', 'section' => 'misc' ); $this->settings['hook_footer_link'] = array( 'title' => ( 'Footer Links' ), 'desc' => ( 'Enter your scripts and html codes' ), 'std' => '', 'type' => 'textarea', 'section' => 'misc' ); /* Reset ===========================================*/ $this->settings['reset_theme'] = array( 'section' => 'reset', 'title' => ( 'Reset theme' ), 'type' => 'checkbox', 'std' => 0, 'class' => 'warning', // Custom class for CSS 'desc' => ( 'Check and click "Save" to reset theme options. This deletes customizations!' ) ); } public function display_section() { // code } public function display_about_section() { // code } /** * Initialize settings to their default values * * @since 1.0 */ public function initialize_settings() { $default_settings = array(); foreach ( $this->settings as $id => $setting ) { if ( $setting['type'] != 'heading' ) $default_settings[$id] = $setting['std']; } update_option( 'asteroid_options', $default_settings ); } /** * Register settings * * @since 1.0 */ public function register_settings() { register_setting( 'asteroid_options', 'asteroid_options', array ( &$this, 'validate_settings' ) ); foreach ( $this->sections as $slug => $title ) { if ( $slug == 'about' ) add_settings_section( $slug, $title, array( &$this, 'display_about_section' ), 'asteroid-options' ); else add_settings_section( $slug, $title, array( &$this, 'display_section' ), 'asteroid-options' ); } $this->get_option(); foreach ( $this->settings as $id => $setting ) { $setting['id'] = $id; $this->create_setting( $setting ); } } /** * jQuery Tabs * * @since 1.0 */ public function scripts() { wp_print_scripts( 'jquery-ui-tabs' ); wp_enqueue_script('media-upload'); wp_enqueue_script('thickbox'); wp_enqueue_script('my-upload'); wp_register_script('my-upload', get_stylesheet_directory_uri() . '/js/uploader.js', array('jquery','media-upload','thickbox')); } /** * Styling for the theme options page * * @since 1.0 */ public function styles() { wp_register_style( 'mytheme-admin', get_stylesheet_directory_uri() . '/library/theme-options.css' ); wp_enqueue_style( 'mytheme-admin' ); wp_enqueue_style('thickbox'); } /** * Validate settings * * @since 1.0 */ public function validate_settings( $input ) { if ( ! isset( $input['reset_theme'] ) ) { $options = get_option( 'asteroid_options' ); foreach ( $this->checkboxes as $id ) { if ( isset( $options[$id] ) && ! isset( $input[$id] ) ) unset( $options[$id] ); } return $input; } return false; } } $theme_options = new My_Theme_Options(); function mytheme_option( $option ) { $options = get_option( 'asteroid_options' ); if ( isset( $options[$option] ) ) return $options[$option]; else return false; }