| <?php |
| function wptuts_get_default_options() { |
| $options = array( |
| 'image1' => '', |
| 'image2' => '' |
| ); |
| return $options; |
| } |
| |
| function wptuts_options_init() { |
| $wptuts_options = get_option( 'theme_wptuts_options' ); |
| |
| |
| if ( false === $wptuts_options ) { |
| |
| $wptuts_options = wptuts_get_default_options(); |
| add_option( 'theme_wptuts_options', $wptuts_options ); |
| } |
| |
| |
| } |
| |
| add_action( 'after_setup_theme', 'wptuts_options_init' ); |
| |
| function wptuts_options_setup() { |
| global $pagenow; |
| if ('media-upload.php' == $pagenow || 'async-upload.php' == $pagenow) { |
| |
| add_filter( 'gettext', 'replace_thickbox_text' , 1, 2 ); |
| } |
| } |
| add_action( 'admin_init', 'wptuts_options_setup' ); |
| |
| function replace_thickbox_text($translated_text, $text ) { |
| if ( 'Insert into Post' == $text ) { |
| $referer = strpos( wp_get_referer(), 'wptuts-settings' ); |
| if ( $referer != '' ) { |
| return __('I want this to be my image!', 'wptuts' ); |
| } |
| } |
| |
| return $translated_text; |
| } |
| |
| |
| function wptuts_menu_options() { |
| |
| add_theme_page('Theme Options', 'Theme Options', 'edit_theme_options', 'wptuts-settings', 'wptuts_admin_options_page'); |
| } |
| |
| add_action('admin_menu', 'wptuts_menu_options'); |
| |
| function wptuts_admin_options_page() { |
| ?> |
| <!-- 'wrap','submit','icon32','button-primary' and 'button-secondary' are classes |
| for a good WP Admin Panel viewing and are predefined by WP CSS --> |
| |
| <div class="wrap"> |
| |
| <div id="icon-themes" class="icon32"><br /></div> |
| |
| <h2><?php _e( 'Theme Options', 'wptuts' ); ?></h2> |
| |
| <!-- If we have any error by submiting the form, they will appear here --> |
| <?php settings_errors( 'wptuts-settings-errors' ); ?> |
| |
| <form id="form-wptuts-options" action="options.php" method="post" enctype="multipart/form-data"> |
| |
| <?php |
| settings_fields('theme_wptuts_options'); |
| do_settings_sections('wptuts'); |
| ?> |
| |
| <p class="submit"> |
| <input name="theme_wptuts_options[submit]" id="submit_options_form" type="submit" class="button-primary" value="<?php esc_attr_e('Save Settings', 'wptuts'); ?>" /> |
| <input name="theme_wptuts_options[reset]" type="submit" class="button-secondary" value="<?php esc_attr_e('Reset Defaults', 'wptuts'); ?>" /> |
| </p> |
| |
| </form> |
| |
| </div> |
| <?php |
| } |
| |
| function wptuts_options_validate( $input ) { |
| |
| $default_options = wptuts_get_default_options(); |
| $valid_input = $default_options; |
| |
| $wptuts_options = get_option('theme_wptuts_options'); |
| |
| $submit = ! empty($input['submit']) ? true : false; |
| $reset = ! empty($input['reset']) ? true : false; |
| $delete_image1 = ! empty($input['delete_image1']) ? true : false; |
| $delete_image2 = ! empty($input['delete_image2']) ? true : false; |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| if ( $submit ) { |
| |
| if($input['image1']){ |
| if ( $wptuts_options['image1'] != $input['image1'] && $wptuts_options['image1'] != '' ){ |
| delete_image( $wptuts_options['image1'] ); |
| |
| |
| } |
| $valid_input['image1'] = $input['image1']; |
| } |
| |
| if($input['image2']){ |
| if ( $wptuts_options['image2'] != $input['image2'] && $wptuts_options['image2'] != '' ){ |
| delete_image( $wptuts_options['image2'] ); |
| |
| |
| } |
| |
| $valid_input['image2'] = $input['image2']; |
| } |
| |
| |
| |
| |
| |
| |
| |
| } |
| elseif ( $reset ) { |
| delete_image( $wptuts_options['image1'] ); |
| $valid_input['image1'] = $default_options['image1']; |
| |
| delete_image( $wptuts_options['image2'] ); |
| $valid_input['image2'] = $default_options['image2']; |
| |
| } |
| elseif ( $delete_image1 ) { |
| delete_image( $wptuts_options['image1'] ); |
| $valid_input['image1'] = ''; |
| } |
| |
| elseif ( $delete_image2 ) { |
| delete_image( $wptuts_options['image2'] ); |
| $valid_input['image2'] = ''; |
| } |
| |
| return $valid_input; |
| } |
| |
| function delete_image( $image_url ) { |
| global $wpdb; |
| |
| |
| $query = "SELECT ID FROM wp_posts where guid = '" . esc_url($image_url) . "' AND post_type = 'attachment'"; |
| $results = $wpdb -> get_results($query); |
| |
| |
| foreach ( $results as $row ) { |
| wp_delete_attachment( $row -> ID ); |
| } |
| } |
| |
| |
| function wptuts_options_enqueue_scripts() { |
| wp_register_script( 'wptuts-upload', get_template_directory_uri() .'/wptuts-options/js/wptuts-upload.js', array('jquery','media-upload','thickbox') ); |
| |
| if ( 'appearance_page_wptuts-settings' == get_current_screen() -> id ) { |
| wp_enqueue_script('jquery'); |
| |
| wp_enqueue_script('thickbox'); |
| wp_enqueue_style('thickbox'); |
| |
| wp_enqueue_script('media-upload'); |
| wp_enqueue_script('wptuts-upload'); |
| |
| } |
| |
| } |
| add_action('admin_enqueue_scripts', 'wptuts_options_enqueue_scripts'); |
| |
| function wptuts_options_settings_init() { |
| register_setting( 'theme_wptuts_options', 'theme_wptuts_options', 'wptuts_options_validate' ); |
| |
| |
| add_settings_section('wptuts_settings_header', __( 'Image Option', 'wptuts' ), 'wptuts_settings_header_text', 'wptuts'); |
| |
| |
| add_settings_field('wptuts_setting_image1', __( 'Image1', 'wptuts' ), 'wptuts_setting_image1', 'wptuts', 'wptuts_settings_header'); |
| |
| |
| add_settings_field('wptuts_setting_image1_preview', __( 'Image1 Preview', 'wptuts' ), 'wptuts_setting_image1_preview', 'wptuts', 'wptuts_settings_header'); |
| |
| |
| |
| |
| add_settings_field('wptuts_setting_image2', __( 'Image2', 'wptuts' ), 'wptuts_setting_image2', 'wptuts', 'wptuts_settings_header'); |
| |
| |
| add_settings_field('wptuts_setting_image2_preview', __( 'Image2 Preview', 'wptuts' ), 'wptuts_setting_image2_preview', 'wptuts', 'wptuts_settings_header'); |
| |
| } |
| add_action( 'admin_init', 'wptuts_options_settings_init' ); |
| |
| function wptuts_settings_header_text() { |
| ?> |
| <p><?php _e( 'Manage Image Options for Kusa Theme.', 'wptuts' ); ?></p> |
| <?php |
| } |
| |
| function wptuts_setting_image1_preview() { |
| $wptuts_options = get_option( 'theme_wptuts_options' ); ?> |
| <div id="upload_image1_preview" style="min-height: 100px;"> |
| <img style="max-width:100%;" src="<?php echo esc_url( $wptuts_options['image1'] ); ?>" /> |
| </div> |
| <?php |
| |
| } |
| |
| function wptuts_setting_image1() { |
| $wptuts_options = get_option( 'theme_wptuts_options' ); |
| ?> |
| <input type="hidden" id="image1_url" name="theme_wptuts_options[image1]" value="<?php echo esc_url( $wptuts_options['image1'] ); ?>" /> |
| <input id="upload_image1_button" type="button" class="button" value="<?php _e( 'Upload image1', 'wptuts' ); ?>" /> |
| <?php if ( '' != $wptuts_options['image1'] ): ?> |
| <input id="delete_image1_button" name="delete_image1" type="submit" class="button" value="<?php _e( 'Delete image1', 'wptuts' ); ?>" /> |
| <input type="hidden" id="image1_url" name="image1" value="1" /> |
| <?php endif; ?> |
| <span class="description"><?php _e('Upload an Image for the banner.', 'wptuts' ); ?></span> |
| <?php |
| } |
| |
| //---------------------image 2---------------------------------------------------------------------------------------------- |
| |
| function wptuts_setting_image2_preview() { |
| $wptuts_options = get_option( 'theme_wptuts_options' ); ?> |
| <div id="upload_image2_preview" style="min-height: 100px;"> |
| <img style="max-width:100%;" src="<?php echo esc_url( $wptuts_options['image2'] ); ?>" /> |
| </div> |
| <?php |
| |
| } |
| |
| function wptuts_setting_image2() { |
| $wptuts_options = get_option( 'theme_wptuts_options' ); |
| ?> |
| <input type="hidden" id="image2_url" name="theme_wptuts_options[image2]" value="<?php echo esc_url( $wptuts_options['image2'] ); ?>" /> |
| <input id="upload_image2_button" type="button" class="button" value="<?php _e( 'Upload image2', 'wptuts' ); ?>" /> |
| <?php if ( '' != $wptuts_options['image2'] ): ?> |
| <input id="delete_image2_button" name="delete_image2" type="submit" class="button" value="<?php _e( 'Delete image2', 'wptuts' ); ?>" /> |
| <input type="hidden" id="image1_url" name="image2" value="2" /> |
| <?php endif; ?> |
| <span class="description"><?php _e('Upload an Image for the banner.', 'wptuts' ); ?></span> |
| <?php |
| } |
| |
| ?> |