add_menu( array( 'parent' => false, 'id' => 'asteroid_admin_bar', 'title' => ('Asteroid Options'), 'href' => admin_url( 'themes.php?page=asteroid-options') )); $wp_admin_bar->add_menu( array( 'parent' => 'appearance', 'id' => 'theme_editor_admin_bar', 'title' => ('Editor'), 'href' => admin_url( 'theme-editor.php') )); $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', 'ast_admin_bar_menu' ); class My_Theme_Options { private $sections; private $checkboxes; private $settings; public function __construct() { // keep track of 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['post-page'] = ( 'Posts & Pages' ); $this->sections['custom-css'] = ( 'Custom CSS' ); $this->sections['custom-widgets'] = ( 'Custom Widgets' ); $this->sections['misc'] = ( 'Misc' ); $this->sections['reset'] = ( 'Reset' ); add_action( 'admin_menu', array( &$this, 'ast_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 ast_add_pages() { $admin_menu = add_theme_page( 'Asteroid Options', 'Asteroid Options', 'edit_theme_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 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 --------------------------------------*/ 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 settings and their defaults --------------------------------------*/ public function get_option() { /* General Settings ===========================================*/ $this->settings['ast_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['ast_post_display_type'] = array( 'section' => 'general', 'title' => ( 'Home Post Display' ), 'desc' => ( 'Show excerpts or full posts on the home page.' ), 'type' => 'radio', 'std' => '1', 'choices' => array( '1' => 'Excerpts', '2' => 'Full Post' ) ); $this->settings['ast_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['ast_hook_footer_links'] = array( 'title' => ( 'Footer Links' ), 'desc' => ( 'Insert your footer links here.    Accepts html codes.' ), 'std' => '', 'type' => 'textarea', 'section' => 'general' ); /* Appearance ===========================================*/ $this->settings['ast_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['ast_favicon'] = array( 'section' => 'appearance', 'title' => ( 'Favicon' ), 'desc' => ( 'The URL of your favicon. It should be 16x16 pixels.' ), 'type' => 'upload', 'std' => '' ); $this->settings['ast_opt_head_2_1'] = array( 'section' => 'appearance', 'title' => ( '' ), 'desc' => ( '' ), 'type' => 'heading', 'std' => '' ); $this->settings['ast_header_height'] = array( 'title' => ( 'Height of Header' ), 'desc' => ( 'px. Set the height of the Header.' ), 'std' => '120', 'type' => 'text-int', 'section' => 'appearance' ); $this->settings['ast_content_width'] = array( 'title' => ( 'Width of Content' ), 'desc' => ( 'px. Set the width of the content/post area.' ), 'std' => '620', 'type' => 'text-int', 'section' => 'appearance' ); $this->settings['ast_sidebar_width'] = array( 'title' => ( 'Width of Sidebar' ), 'desc' => ( 'px. Set the width of the Sidebar.' ), 'std' => '310', 'type' => 'text-int', 'section' => 'appearance' ); $this->settings['ast_header_bgcolor'] = array( 'title' => ( 'Color of Header' ), 'desc' => ( 'Choose a background color for the #header container.' ), 'std' => 'FFFFFF', 'type' => 'color', 'section' => 'appearance' ); $this->settings['ast_content_bgcolor'] = array( 'title' => ( 'Color of Content' ), 'desc' => ( 'Choose a background color for the #content container.' ), 'std' => 'FFFFFF', 'type' => 'color', 'section' => 'appearance' ); $this->settings['ast_sidebar_bgcolor'] = array( 'title' => ( 'Color of Sidebar' ), 'desc' => ( 'Choose a background color for the #sidebar container.' ), 'std' => 'FFFFFF', 'type' => 'color', 'section' => 'appearance' ); $this->settings['ast_opt_head2'] = array( 'section' => 'appearance', 'title' => ( '' ), 'desc' => ( '' ), 'type' => 'heading', 'std' => 0 ); /* Posts & Pages ===========================================*/ $this->settings['ast_excerpt_thumbnails'] = array( 'section' => 'post-page', 'title' => ( 'Excerpt Thumbnails' ), 'desc' => ( 'Show Thumbnails on excerpts. Featured image will be used.' ), 'type' => 'checkbox', 'std' => 1 ); $this->settings['ast_blog_date'] = array( 'section' => 'post-page', 'title' => ( 'Publish Date on Home' ), 'desc' => ( 'Show Publish Date on Blog, Archives, Searches and Excerpts.' ), 'type' => 'checkbox', 'std' => 0 ); $this->settings['ast_post_date'] = array( 'section' => 'post-page', 'title' => ( 'Post Publish Date' ), 'desc' => ( 'Show Publish Date on Single Posts.' ), 'type' => 'checkbox', 'std' => 1 ); $this->settings['ast_post_author'] = array( 'section' => 'post-page', 'title' => ( 'Post Author' ), 'desc' => ( 'Show the Author’s name on Posts.' ), 'type' => 'checkbox', 'std' => 1 ); $this->settings['ast_page_date'] = array( 'section' => 'post-page', 'title' => ( 'Page Publish Date' ), 'desc' => ( 'Show Publish Date on Single Pages' ), 'type' => 'checkbox', 'std' => 0 ); $this->settings['ast_page_author'] = array( 'section' => 'post-page', 'title' => ( 'Page Author' ), 'desc' => ( 'Show the Author’s name on Pages.' ), 'type' => 'checkbox', 'std' => 0 ); $this->settings['ast_date_modified'] = array( 'section' => 'post-page', 'title' => ( 'Show Date Modified' ), 'desc' => ( 'Show the date when the Post or Page was modified.' ), 'type' => 'select', 'std' => 1, 'choices' => array( 0 => 'Hidden', 1 => 'On Posts', 2 => 'On Pages', 3 => 'Both Posts & Pages' ) ); $this->settings['ast_post_comments'] = array( 'section' => 'post-page', 'title' => ( 'Post Comments' ), 'desc' => ( 'Show the comments and comment form on Posts.' ), 'type' => 'checkbox', 'std' => 1 ); $this->settings['ast_page_comments'] = array( 'section' => 'post-page', 'title' => ( 'Page Comments' ), 'desc' => ( 'Show the comments and comment form on Pages.' ), 'type' => 'checkbox', 'std' => 1 ); /* Custom CSS ===========================================*/ $this->settings['ast_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' ); /* Custom Widgets ===========================================*/ $this->settings['ast_widget_body'] = array( 'section' => 'custom-widgets', 'title' => ( 'Widgets on Body' ), 'desc' => ( 'Allow widgets on the Body' ), 'type' => 'checkbox', 'std' => 0 ); $this->settings['ast_widget_header'] = array( 'section' => 'custom-widgets', 'title' => ( 'Widgets on Header' ), 'desc' => ( 'Allow widgets on the Header' ), 'type' => 'checkbox', 'std' => 0 ); $this->settings['ast_widget_below_menu'] = array( 'section' => 'custom-widgets', 'title' => ( 'Widgets Below Menu' ), 'desc' => ( 'Allow widgets below the main menu.' ), 'type' => 'checkbox', 'std' => 0 ); $this->settings['ast_opt_head_5_1'] = array( 'section' => 'custom-widgets', 'title' => ( '' ), 'desc' => ( '' ), 'type' => 'heading', 'std' => '' ); $this->settings['ast_widget_before_post'] = array( 'section' => 'custom-widgets', 'title' => ( 'Widgets Before Post' ), 'desc' => ( 'Allow widgets to show after the post-title.' ), 'type' => 'checkbox', 'std' => 0 ); $this->settings['ast_widget_before_post_content'] = array( 'section' => 'custom-widgets', 'title' => ( 'Widgets Before Post - Content' ), 'desc' => ( 'Allow widgets to show before the post-content.' ), 'type' => 'checkbox', 'std' => 0 ); $this->settings['ast_widget_after_post_content'] = array( 'section' => 'custom-widgets', 'title' => ( 'Widgets After Post - Content' ), 'desc' => ( 'Allow widgets to show after the post-content.' ), 'type' => 'checkbox', 'std' => 0 ); $this->settings['ast_widget_after_post'] = array( 'section' => 'custom-widgets', 'title' => ( 'Widgets After Post' ), 'desc' => ( 'Allow widgets to show at the post-footer.' ), 'type' => 'checkbox', 'std' => 0 ); $this->settings['ast_opt_head_5_2'] = array( 'section' => 'custom-widgets', 'title' => ( '' ), 'desc' => ( '' ), 'type' => 'heading', 'std' => '' ); $this->settings['ast_widget_before_page'] = array( 'section' => 'custom-widgets', 'title' => ( 'Widgets Before Page' ), 'desc' => ( 'Allow widgets to show after the page-title.' ), 'type' => 'checkbox', 'std' => 0 ); $this->settings['ast_widget_before_page_content'] = array( 'section' => 'custom-widgets', 'title' => ( 'Widgets Before Page - Content' ), 'desc' => ( 'Allow widgets to show before the page-content.' ), 'type' => 'checkbox', 'std' => 0 ); $this->settings['ast_widget_after_page_content'] = array( 'section' => 'custom-widgets', 'title' => ( 'Widgets After Page - Content' ), 'desc' => ( 'Allow widgets to show after the page-content.' ), 'type' => 'checkbox', 'std' => 0 ); $this->settings['ast_widget_after_page'] = array( 'section' => 'custom-widgets', 'title' => ( 'Widgets After Page' ), 'desc' => ( 'Allow widgets to show at the page-footer.' ), 'type' => 'checkbox', 'std' => 0 ); /* Miscellaneous ===========================================*/ $this->settings['ast_remove_wp_version'] = array( 'section' => 'misc', 'title' => ( 'Remove WordPress Version' ), 'desc' => ( 'Prevent WP Version from being displayed in the <Head>' ), 'type' => 'checkbox', 'std' => 0 ); $this->settings['ast_remove_theme_link'] = array( 'section' => 'misc', 'title' => ( 'Remove Theme link' ), 'desc' => ( 'Remove the Asteroid Theme link in the footer.' ), 'type' => 'checkbox', 'std' => 0 ); /* Reset ===========================================*/ $this->settings['ast_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!' ) ); } /*------------------------------------- Description for pages --------------------------------------*/ public function display_section() { // No Description } public function display_about_section() { // No Description } /*------------------------------------- Initialize Settings to Defaults --------------------------------------*/ 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 --------------------------------------*/ 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 --------------------------------------*/ 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 --------------------------------------*/ 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 --------------------------------------*/ public function validate_settings( $input ) { if ( ! isset( $input['ast_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; }