Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /**
- * Store our table name in $wpdb with correct prefix
- */
- function st_register_table() {
- global $wpdb;
- $wpdb->st = "{$wpdb->prefix}st";
- } // end st_register_table
- add_action( 'init', 'st_register_table', 1 );
- add_action( 'switch_blog', 'st_register_table' );
- /**
- * Creating / Deleting Custom Database Table
- */
- function st_theme_activation() {
- global $wpdb;
- global $charset_collate;
- // Call this manually as we may have missed the init hook
- st_register_table();
- if ( $wpdb->get_var('SHOW TABLES LIKE ' . $wpdb->st ) != $wpdb->st ) {
- $query = "CREATE TABLE " . $wpdb->st . "(
- option_id BIGINT(20) UNSIGNED AUTO_INCREMENT,
- option_name VARCHAR(64),
- option_value LONGTEXT,
- autoload VARCHAR(20) DEFAULT 'yes',
- PRIMARY KEY (option_id) )";
- require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
- dbDelta($query);
- add_option( 'st_database_version', '1.0' );
- }
- } // end st_theme_activation
- add_action( 'after_switch_theme', 'st_theme_activation' );
- function st_get_table_columns() {
- return array(
- 'option_id' => '%d',
- 'option_name' => '%s',
- 'option_value' => '%s',
- 'autoload' => '%s',
- );
- } // end st_get_table_columns
- function st_theme_deactivation() {
- global $wpdb;
- // Dropping Custom Database Table
- $query = "DROP TABLE IF EXISTS {$wpdb->st}";
- $wpdb->query($query);
- // Dropping option
- delete_option( 'st_database_version' );
- } // end st_theme_deactivation
- add_action( 'switch_theme', 'st_theme_deactivation' );
- /**
- * Theme Options Page
- */
- function stheme_menu() {
- $paged = add_theme_page(
- 's Theme',
- 's Theme',
- 'administrator',
- 'stheme_theme_options',
- 'stheme_theme_display'
- );
- add_action( 'admin_print_scripts-' . $paged, 'stheme_print_scripts' );
- add_action( 'admin_print_styles-' . $paged, 'stheme_admin_scripts' );
- $stheme_options_page = add_menu_page(
- __('s Theme', 'stheme'),
- __('s Theme', 'stheme'),
- 'administrator',
- 'stheme_theme_menu',
- 'stheme_theme_display',
- get_template_directory_uri().'/img/st-favicon.png'
- );
- add_action( 'admin_print_scripts-' . $stheme_options_page, 'stheme_print_scripts' );
- $stheme_general_options_page = add_submenu_page(
- 'stheme_theme_menu',
- __('General Options', 'stheme'),
- __('General Options', 'stheme'),
- 'administrator',
- 'stheme_theme_general_options',
- create_function(null, 'stheme_theme_display("general_options");')
- );
- add_action( 'admin_print_scripts-' . $stheme_general_options_page, 'stheme_print_scripts' );
- add_submenu_page(
- 'stheme_theme_menu',
- __('Homepage Options', 'stheme'),
- __('Homepage Options', 'stheme'),
- 'administrator',
- 'stheme_homepage_options',
- create_function(null, 'stheme_theme_display("homepage_options");' )
- );
- add_submenu_page(
- 'stheme_theme_menu',
- __('Footer Options', 'stheme'),
- __('Footer Options', 'stheme'),
- 'administrator',
- 'stheme_footer_options',
- create_function(null, 'stheme_theme_display("footer_options");')
- );
- } // end stheme_theme_menu
- add_action('admin_menu', 'stheme_menu');
- /*==============================================================================================================*/
- /**
- * Renders a simple page to display for the theme menu defined above.
- */
- function stheme_theme_display( $active_tab = null ) {
- ?>
- <!-- Create a header in the default WordPress 'wrap' container -->
- <div class="wrap">
- <?php screen_icon( 'themes' ); ?>
- <h2>s Theme Options</h2>
- <?php settings_errors(); ?>
- <?php
- if ( isset($_GET['tab']) ) {
- $active_tab = $_GET['tab'];
- } else if ( $active_tab == 'homepage_options') {
- $active_tab = 'homepage_options';
- } else if ( $active_tab == 'footer_options') {
- $active_tab = 'footer_options';
- } else {
- $active_tab = 'general_options';
- } // end if else
- ?>
- <h2 class="nav-tab-wrapper">
- <a href="?page=stheme_theme_options&tab=general_options" class="nav-tab <?php echo $active_tab == 'general_options' ? 'nav-tab-active' : '' ?>"><?php _e('General', 'stheme'); ?></a>
- <a href="?page=stheme_theme_options&tab=homepage_options" class="nav-tab <?php echo $active_tab == 'homepage_options' ? 'nav-tab-active' : '' ?>"><?php _e('Homepage', 'stheme'); ?></a>
- <a href="?page=stheme_theme_options&tab=footer_options" class="nav-tab <?php echo $active_tab == 'footer_options' ? 'nav-tab-active' : '' ?>"><?php _e('Footer', 'stheme'); ?></a>
- </h2>
- <form method="POST" action="options.php">
- <?php
- if ( $active_tab == 'general_options' ) {
- settings_fields('stheme_theme_general_options');
- do_settings_sections('stheme_theme_general_options');
- } else if ( $active_tab == 'homepage_options' ) {
- settings_fields('stheme_homepage_options');
- do_settings_sections('stheme_homepage_options');
- } else if ( $active_tab == 'footer_options' ) {
- settings_fields('stheme_footer_options');
- do_settings_sections('stheme_footer_options');
- }
- submit_button();
- ?>
- </form>
- </div> <!-- /.wrap -->
- <?php
- } // end stheme_theme_display
- /*==============================================================================================================*/
- /**
- * General Options
- */
- // Set Options for General Options
- function stheme_default_options() {
- $defaults = array(
- 'logo' => 'test1',
- 'favicon' => 'test2',
- 'top_nav' => 'yes'
- );
- return apply_filters( 'stheme_default_options', $defaults );
- } // end stheme_default_options
- /*==============================================================================================================*/
- // Set Options for Homepage Options
- function stheme_homepage_default_options() {
- $defaults = array(
- 'show_homepage_blog' => 'yes',
- 'show_sidebar' => 'yes',
- 'show_footer' => 'yes'
- );
- return apply_filters( 'stheme_homepage_default_options', $defaults );
- } // end stheme_homepage_default_options
- /*==============================================================================================================*/
- // Set Options for Footer Options
- function stheme_footer_default_options() {
- $defaults = array(
- 'footer_text_left' => 'STheme',
- 'footer_text_right' => 'This is where you\'ll put the media icons.'
- );
- return apply_filters( 'stheme_footer_default_options', $defaults );
- } // end stheme_footer_default_options
- /*==============================================================================================================*/
- // Initialization for General Options
- function stheme_initialize_theme_options() {
- global $wpdb;
- $data = array();
- // Collect the default values
- $default = stheme_default_options();
- // Set default values
- $data = wp_parse_args( $data, array(
- 'option_name' => 'stheme_general_options',
- ));
- // Initialise column format array
- $column_formats = st_get_table_columns();
- $inserted = $wpdb->insert(
- $wpdb->st,
- array(
- 'option_id' => '',
- 'option_name' => 'stheme_theme_general_options',
- 'option_value' => apply_filters( 'stheme_default_options', stheme_default_options() ),
- 'autoload' => ''
- ),
- $column_formats
- );
- // Register new options
- add_settings_section(
- 'stheme_section',
- __('General Options', 'stheme'),
- 'stheme_section_callback',
- 'stheme_theme_general_options'
- );
- // Settings for logo
- add_settings_field(
- 'stheme_logo',
- __('sTheme Logo', 'stheme'),
- 'stheme_setting_logo_callback',
- 'stheme_theme_general_options',
- 'stheme_section'
- );
- // Settings for Favicon
- add_settings_field(
- 'stheme_favicon',
- __('sTheme Favicon', 'stheme'),
- 'stheme_setting_favicon_callback',
- 'stheme_theme_general_options',
- 'stheme_section'
- );
- // Setting for Top Navigation Position
- add_settings_field(
- 'stheme_top_nav_position',
- __('sTheme Top Navigation Position', 'stheme'),
- 'stheme_setting_top_nav_position_callback',
- 'stheme_theme_general_options',
- 'stheme_section'
- );
- register_setting(
- 'stheme_theme_general_options',
- 'stheme_theme_general_options',
- 'stheme_theme_validate'
- );
- } // end stheme_initialize_theme_options
- add_action('admin_init', 'stheme_initialize_theme_options');
- /*=============================================================================================================*/
- // Initialization for Homepage Options
- function stheme_homepage_initialize_theme_options() {
- global $wpdb;
- // Initialise column format array
- $column_formats = st_get_table_columns();
- $inserted = $wpdb->insert(
- $wpdb->st,
- array(
- 'option_id' => '',
- 'option_name' => 'stheme_homepage_options',
- 'option_value' => apply_filters( 'stheme_homepage_default_options', stheme_footer_default_options() ),
- 'autoload' => ''
- ),
- $column_formats
- );
- add_settings_section(
- 'stheme_homepage_section',
- __('Homepage Options', 'stheme'),
- 'stheme_homepage_callback',
- 'stheme_homepage_options'
- );
- add_settings_field(
- 'stheme_show_homepage_blog',
- __('Homepage Blog', 'stheme'),
- 'stheme_setting_show_blog_callback',
- 'stheme_homepage_options',
- 'stheme_homepage_section',
- array(
- __('Activate this to display the Homepage Blog.', 'stheme'),
- )
- );
- add_settings_field(
- 'stheme_show_sidebar',
- __('Sidebar', 'stheme'),
- 'stheme_setting_show_sidebar_callback',
- 'stheme_homepage_options',
- 'stheme_homepage_section',
- array(
- __('Activate this to display the Page Sidebar.', 'stheme'),
- )
- );
- add_settings_field(
- 'stheme_show_masthead',
- __('Masthead', 'stheme'),
- 'stheme_setting_show_masthead_callback',
- 'stheme_homepage_options',
- 'stheme_homepage_section',
- array(
- __('Activate this to display the Homepage Masthead.', 'stheme'),
- )
- );
- add_settings_field(
- 'stheme_show_featurette_f',
- __('Featurette First', 'stheme'),
- 'stheme_setting_show_featurette_f_callback',
- 'stheme_homepage_options',
- 'stheme_homepage_section',
- array(
- __('Activate this to display the Homepage First Featurette.', 'stheme'),
- )
- );
- add_settings_field(
- 'stheme_show_featurette_s',
- __('Featurette Second', 'stheme'),
- 'stheme_setting_show_featurette_s_callback',
- 'stheme_homepage_options',
- 'stheme_homepage_section',
- array(
- __('Activate this to display the Homepage Second Featurette', 'stheme'),
- )
- );
- register_setting(
- 'stheme_homepage_options',
- 'stheme_homepage_options'
- );
- } // end stheme_homepage_initialize_theme_options
- add_action('admin_init', 'stheme_homepage_initialize_theme_options');
- /*==============================================================================================================*/
- // Initializations for Footer Options
- function stheme_footer_initialize_theme_options() {
- global $wpdb;
- // Initialise column format array
- $column_formats = st_get_table_columns();
- $inserted = $wpdb->insert(
- $wpdb->st,
- array(
- 'option_id' => '',
- 'option_name' => 'stheme_footer_options',
- 'option_value' => apply_filters( 'stheme_footer_default_options', stheme_footer_default_options() ),
- 'autoload' => ''
- ),
- $column_formats
- );
- add_settings_section(
- 'stheme_footer_section',
- __('Footer Options', 'stheme'),
- 'stheme_footer_callback',
- 'stheme_footer_options'
- );
- add_settings_field(
- 'stheme_footer_text_left',
- __('Footer Text Left', 'stheme'),
- 'stheme_setting_footer_text_left_callback',
- 'stheme_footer_options',
- 'stheme_footer_section'
- );
- add_settings_field(
- 'stheme_footer_text_right',
- __('Footer Text Right', 'stheme'),
- 'stheme_setting_footer_text_right_callback',
- 'stheme_footer_options',
- 'stheme_footer_section'
- );
- register_setting(
- 'stheme_footer_options',
- 'stheme_footer_options',
- 'stheme_theme_input_validate'
- );
- } // end stheme_footer_initialize_theme_options
- add_action('admin_init', 'stheme_footer_initialize_theme_options');
- /*==============================================================================================================*/
- /**
- * CallBacks
- */
- /**
- * This function provides a simple description for the General Options page.
- *
- * It's called from the 'stheme_initialize_theme_options' function by being passed as a parameter
- * in the add_settings_section function.
- */
- function stheme_section_callback() {
- echo '<p>'.__( 'General settings for your s Theme.', 'stheme' ).'</p>';
- } // end stheme_general_options_callback
- /**
- * This function provides a simple description for the Homepage Options page.
- *
- * It's called from the 'stheme_homepage_initialize_theme_options' function by being passed as a parameter
- * in the add_settings_section function.
- */
- function stheme_homepage_callback() {
- echo '<p>' . __( 'Select which areas of content you wish to display.', 'stheme' ) . '</p>';
- } // end stheme_general_callback
- /**
- * This function provides a simple description for the Footer Options page.
- *
- * It's called from the 'stheme_footer_initialize_theme_options' function by being passed as a parameter
- * in the add_settings_section function.
- */
- function stheme_footer_callback() {
- echo '<p>' . __( 'Type text for Footer.', 'stheme' ) . '</p>';
- } // end stheme_footer_callback
- /*==============================================================================================================*/
- /* General Logo Callback */
- function stheme_setting_logo_callback() {
- $options = get_option('stheme_theme_general_options');
- ?>
- <span class="upload">
- <input type="text" id="stheme_logo" class="regular-text text-upload" name="stheme_theme_general_options[logo]" value="<?php echo esc_url( $options['logo'] ); ?>" />
- <input type="button" class="button button-upload" value="Upload an image" /><br>
- <img style="max-width: 300px display: block;" src="<?php echo esc_url($options['logo']); ?>" class="preview-upload" />
- </span>
- <?php
- } // end stheme_setting_logo_callback
- /* General Favicon Callback */
- function stheme_setting_favicon_callback() {
- $options = get_option('stheme_theme_general_options');
- ?>
- <span class="upload">
- <input type="text" id="stheme_favicon" class="regular-text text-upload" name="stheme_theme_general_options[favicon]" value="<?php echo esc_url( $options["favicon"] ); ?>" />
- <input type="button" class="button button-upload" value="Upload an image"/></br>
- <img style="max-width: 300px; display: block;" src="<?php echo esc_url( $options["favicon"] ); ?>" class="preview-upload" />
- </span>
- <?php
- } // end stheme_setting_logo_preview_callback
- /* General Top Navigation Position */
- function stheme_setting_top_nav_position_callback() {
- $options = get_option('stheme_theme_general_options');
- $html = '<select id="stheme_top_nav_position" name="stheme_theme_general_options[stheme_top_nav_position]">';
- $html .= '<option value="default">' . __('Select Top Navigation Position..', 'stheme') . '</option>';
- $html .= '<option value="left"' . selected( $options['stheme_top_nav_position'], 'http://left', false) . '>' . __('Left Position', 'stheme') . '</option>';
- $html .= '<option value="bottom"' . selected( $options['stheme_top_nav_position'], 'http://bottom', false) . '>' . __('Bottom Position', 'stheme') . '</option>';
- $html .= '</select>';
- echo $html;
- } // stheme_setting_top_nav_position_callback
- /*==============================================================================================================*/
- /* Homepage Show Blog Callback */
- function stheme_setting_show_blog_callback( $args ) {
- // First, we need to read the options selections
- $options = get_option('stheme_homepage_options');
- // Next, we update the name attribute to access this element's ID in the context tof the display options array
- // We also access the stheme_setting_show_blog_callback element of the options collection in the call to the checked() helper function
- $html = '<input type="checkbox" id="stheme_show_homepage_blog" name="stheme_homepage_options[stheme_show_homepage_blog]" value="1" ' . checked( 1, isset( $options['stheme_show_homepage_blog'] ) ? $options['stheme_show_homepage_blog'] : 0, false ) . '/>';
- // Here, we'll take the first argument of the array and add it to a label next to the checkbox
- $html .= '<label for="stheme_show_homepage_blog"> ' . $args[0] . '</label>';
- // Display this to Browser
- echo $html;
- } // end stheme_setting_show_blog_callback
- /* Homepage Show Page Sidebar Callback */
- function stheme_setting_show_sidebar_callback( $args ) {
- $options = get_option('stheme_homepage_options');
- $html = '<input type="checkbox" id="stheme_show_sidebar" name="stheme_homepage_options[stheme_show_sidebar]" value="1" ' . checked( 1, isset( $options['stheme_show_sidebar'] ) ? $options['stheme_show_sidebar'] : 0, false ) . '/>';
- $html .= '<label for="stheme_show_sidebar"> ' . $args[0] . '</label>';
- echo $html;
- } // end stheme_setting_show_sidebar_callback
- /* Homepage Show Masthead Callback */
- function stheme_setting_show_masthead_callback( $args ) {
- $options = get_option('stheme_homepage_options');
- $html = '<input type="checkbox" id="stheme_show_masthead" name="stheme_homepage_options[stheme_show_masthead]" value="1" ' . checked( 1, isset( $options['stheme_show_masthead'] ) ? $options['stheme_show_masthead'] : 0, false ) . '/>';
- $html .= '<label for="stheme_show_masthead"> ' . $args[0] . '</label>';
- echo $html;
- } // end stheme_setting_show_masthead_callback
- /* Homepage Show Featurette First Callback */
- function stheme_setting_show_featurette_f_callback( $args ) {
- $options = get_option('stheme_homepage_options');
- $html = '<input type="checkbox" id="stheme_setting_show_featurette_f_callback" name="stheme_homepage_options[stheme_setting_show_featurette_f_callback]" value="1" ' . checked( 1, isset( $options['stheme_setting_show_featurette_f_callback'] ) ? $options['stheme_setting_show_featurette_f_callback'] : 0, false ) . '/>';
- $html .= '<label for="stheme_setting_show_featurette_f_callback"> ' . $args[0] . '</label>';
- echo $html;
- } // end stheme_setting_show_featurette_f_callback
- /* Homepage Show Featurette Second Callback */
- function stheme_setting_show_featurette_s_callback( $args ) {
- $options = get_option('stheme_homepage_options');
- $html = '<input type="checkbox" id="stheme_setting_show_featurette_s_callback" name="stheme_homepage_options[stheme_setting_show_featurette_s_callback]" value="1" ' . checked( 1, isset( $options['stheme_setting_show_featurette_s_callback'] ) ? $options['stheme_setting_show_featurette_s_callback'] : 0, false ) . '/>';
- $html .= '<label for="stheme_setting_show_featurette_s_callback"> ' . $args[0] . '</label>';
- echo $html;
- } // end stheme_setting_show_featurette_s_callback
- /*===============================================================================================================*/
- /* Footer Text Left Callback */
- function stheme_setting_footer_text_left_callback() {
- $options = get_option('stheme_footer_options');
- echo '<input type="text" id="stheme_footer_text_left" name="stheme_footer_options[stheme_footer_text_left]" value="' . $options['stheme_footer_text_left'] . '" />';
- } // end stheme_setting_footer_text_left_callback
- /* Footer Text Right Callback */
- function stheme_setting_footer_text_right_callback() {
- $options = get_option('stheme_footer_options');
- echo '<input type="text" id="stheme_footer_text_right" name="stheme_footer_options[stheme_footer_text_right]" value="' . $options['stheme_footer_text_right'] . '" />';
- } // end stheme_setting_footer_text_right_callback
- /*==============================================================================================================*/
- /* Add favico to site */
- function stheme_add_favicon() {
- $stheme_options = get_option( 'stheme_theme_general_options' );
- $stheme_favicon = $stheme_options['favicon'];
- $favicon = $stheme_options['favicon'] != '' ? $stheme_favicon : get_template_directory_uri().'/img/st-favicon.png';
- ?>
- <link rel="icon" type="image/png" href="<?php echo esc_url( $favicon ); ?>">
- <?php
- } // end stheme_add_favicon
- add_action('wp_head', 'stheme_add_favicon');
- /**
- * Validate
- */
- function stheme_theme_validate( $values ) {
- foreach ( $values as $n => $v )
- $values[$n] = esc_url( $v );
- return $values;
- } // end stheme_theme_validate
- function stheme_theme_input_validate( $input ) {
- // Create our array for storing the validated options
- $output = array();
- // Loop through each of the incoming options
- foreach( $input as $key => $value ) {
- // Check to see if the current option has a value. If so, process it.
- if( isset( $input[$key] ) ) {
- // Strip all HTML and PHP tags and properly handle quoted strings
- $output[$key] = strip_tags( stripslashes( $input[ $key ] ) );
- } // end if
- } // end foreach
- // Return the array processing any additional functions filtered by this action
- return apply_filters( 'stheme_theme_input_validate', $output, $input );
- } // end stheme_theme_input_validate
- function stheme_theme_social_validate( $input ) {
- // Define the array for the updated options
- $output = array();
- // Loop through each of the options sanitizing the data
- foreach( $input as $key => $val ) {
- if( isset ( $input[$key] ) ) {
- $output[$key] = esc_url_raw( strip_tags( stripslashes( $input[$key] ) ) );
- } // end if
- } // end foreach
- // Return the new collection
- return apply_filters( 'sandbox_theme_sanitize_social_options', $output, $input );
- } // end stheme_theme_social_validate
- /**
- * Embed Scripts
- */
- function stheme_print_scripts() {
- wp_enqueue_style( 'thickbox' ); // Stylesheet used by Thickbox
- wp_enqueue_script( 'thickbox' );
- wp_enqueue_script( 'media-upload' );
- wp_enqueue_script( 'stheme-upload', get_template_directory_uri().'/st-options/js/st-upload.js', array('thickbox', 'media-upload') );
- } // end stheme_print_scripts
- function stheme_admin_scripts() {
- wp_enqueue_style( 'farbtastic' );
- wp_enqueue_script( 'farbtastic' );
- wp_enqueue_script( 'stheme_theme_style_options', get_template_directory_uri() . '/st-options/js/st-colourpick.js', array('farbtastic', 'jquery') );
- } // end stheme_admin_scripts
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement