• Gini master” semua ane kan bikin theme option tuh
    nah waktu upload 2″nya tu lancar jaya luar biasa
    tapi ketika ane delete salah satu image…eh malah imagenya ke delete semua…
    tolong bantuin ane gan…gmn caranya agar ketika ane delete satu image ygke delete ya itu saja..ndak semua ke delete..

    Tolong Master” semua

    ini functionnya

    <?php
    function wptuts_get_default_options() {
    	$options = array(
    		'image1' => '',
    		'image2' => ''
    	);
    	return $options;
    }
    
    function wptuts_options_init() {
         $wptuts_options = get_option( 'theme_wptuts_options' );
    
    	 // Are our options saved in the DB?
         if ( false === $wptuts_options ) {
    		  // If not, we'll save our default options
              $wptuts_options = wptuts_get_default_options();
    		  add_option( 'theme_wptuts_options', $wptuts_options );
         }
    
         // In other case we don't need to update the DB
    }
    // Initialize Theme options
    add_action( 'after_setup_theme', 'wptuts_options_init' );
    
    function wptuts_options_setup() {
    	global $pagenow;
    	if ('media-upload.php' == $pagenow || 'async-upload.php' == $pagenow) {
    		// Now we'll replace the 'Insert into Post Button inside Thickbox'
    		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;
    }
    
    // Add "WPTuts Options" link to the "Appearance" menu
    function wptuts_menu_options() {
    	//add_theme_page( $page_title, $menu_title, $capability, $menu_slug, $function);
         add_theme_page('Theme Options', 'Theme Options', 'edit_theme_options', 'wptuts-settings', 'wptuts_admin_options_page');
    }
    // Load the 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($_POST['delete_image1'] == 'Delete image1' && $_POST['delete_image2'] == '') {
    	// 	delete_image( $wptuts_options['image1'] );
    	// }
    	// elseif($_POST['delete_image2'] == 'Delete image2' && $_POST['delete_image1'] == '') {
    	// 	delete_image( $wptuts_options['image2'] );
    
    	// }
    
    	if ( $submit ) {
    
    		if($input['image1']){
    			if ( $wptuts_options['image1'] != $input['image1']  && $wptuts_options['image1'] != '' ){
    			delete_image( $wptuts_options['image1'] );
    			// var_dump($input['image1']);
    			// die();
    			}
    			$valid_input['image1'] = $input['image1'];
    		}
    
    		if($input['image2']){
    			if ( $wptuts_options['image2'] != $input['image2']  && $wptuts_options['image2'] != '' ){
    			delete_image( $wptuts_options['image2'] );
    			// var_dump($input['image2']);
    			// die();
    			}
    
    			$valid_input['image2'] = $input['image2'];
    		}
    
    		// if ($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;
    
    	// We need to get the image's meta ID..
    	$query = "SELECT ID FROM wp_posts where guid = '" . esc_url($image_url) . "' AND post_type = 'attachment'";
    	$results = $wpdb -> get_results($query);
    
    	// And delete them (if more than one attachment is in the Library
    	foreach ( $results as $row ) {
    		wp_delete_attachment( $row -> ID );
    	}
    }
    
    /********************* JAVASCRIPT ******************************/
    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 a form section for the image1
    	add_settings_section('wptuts_settings_header', __( 'Image Option', 'wptuts' ), 'wptuts_settings_header_text', 'wptuts');
    
    	// Add image1 uploader
    	add_settings_field('wptuts_setting_image1',  __( 'Image1', 'wptuts' ), 'wptuts_setting_image1', 'wptuts', 'wptuts_settings_header');
    
    	// Add Current Image Preview
    	add_settings_field('wptuts_setting_image1_preview',  __( 'Image1 Preview', 'wptuts' ), 'wptuts_setting_image1_preview', 'wptuts', 'wptuts_settings_header');
    
    	//-------------------image2---------------------------------------------------------------------------------------
    
    	// Add image2 uploader
    	add_settings_field('wptuts_setting_image2',  __( 'Image2', 'wptuts' ), 'wptuts_setting_image2', 'wptuts', 'wptuts_settings_header');
    
    	// Add Current Image Preview
    	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
    }
    
    ?>

    ini Jquerynya

    jQuery(document).ready(function($){
    	$('#upload_image1_button').click(function() {
    		//type = image,audio,video,file. If we write it wrong, nothing will appear. type = file by default
    		// El tipo no importa, ya que desde hace algunas versiones, el uploader puede subir cualquier tipo de archivo
    
    		// Si no lo hacemos desde un meta box dentro de un post y además WP_DEBUG = true, nos saldrá un error ya que
    		// no estará asociado a ningún post
    
    		//tb_show(caption, url, imageGroup)
    		// Google: 'ImageGroup tb_show thickbox':
    		//The optional imageGroup parameter can also be used to pass in an array of images for a single or multiple image slide show gallery.
    		// The problem is that inserting a gallery needs an associated post to work
    		tb_show('Upload a image1', 'media-upload.php?referer=wptuts-settings&type=image&TB_iframe=true&post_id=0', false);
    
    		window.send_to_editor = function(html) {
    		// html returns a link like this:
    		// <a href="{server_uploaded_image_url}"><img src="{server_uploaded_image_url}" alt="" title="" width="" height"" class="alignzone size-full wp-image-125" /></a>
    		var image_url = $('img',html).attr('src');
    
    		//alert(html);
    		$('#image1_url').val(image_url);
    		tb_remove();
    		$('#upload_image1_preview img').attr('src',image_url);
    
    		$('#submit_options_form').trigger('click');
    		// $('#uploaded_image1').val('uploaded');
    		}
    
    		return false;
    
    	});
    
    	$('#upload_image2_button').click(function() {
    		//type = image,audio,video,file. If we write it wrong, nothing will appear. type = file by default
    		// El tipo no importa, ya que desde hace algunas versiones, el uploader puede subir cualquier tipo de archivo
    
    		// Si no lo hacemos desde un meta box dentro de un post y además WP_DEBUG = true, nos saldrá un error ya que
    		// no estará asociado a ningún post
    
    		//tb_show(caption, url, imageGroup)
    		// Google: 'ImageGroup tb_show thickbox':
    		//The optional imageGroup parameter can also be used to pass in an array of images for a single or multiple image slide show gallery.
    		// The problem is that inserting a gallery needs an associated post to work
    		tb_show('Upload a image2', 'media-upload.php?referer=wptuts-settings&type=image&TB_iframe=true&post_id=0', false);
    
    		window.send_to_editor = function(html) {
    		// html returns a link like this:
    		// <a href="{server_uploaded_image_url}"><img src="{server_uploaded_image_url}" alt="" title="" width="" height"" class="alignzone size-full wp-image-125" /></a>
    		var image_url = $('img',html).attr('src');
    
    		//alert(html);
    		$('#image2_url').val(image_url);
    		tb_remove();
    		$('#upload_image2_preview img').attr('src',image_url);
    
    		$('#submit_options_form').trigger('click');
    		// $('#uploaded_image2').val('uploaded');
    		}
    
    		return false;
    
    	});
    });

Melihat 2 balasan - 1 sampai 2 (dari total 2)
  • Coba dengan sedikit pergantian code berikut ini:

    <?php
    function wptuts_get_default_options() {
    	$options = array(
    		'logo' => ''
    	);
    	return $options;
    }
    
    function wptuts_options_init() {
         $wptuts_options = get_option( 'theme_wptuts_options' );
    
    	 // Are our options saved in the DB?
         if ( false === $wptuts_options ) {
    		  // If not, we'll save our default options
              $wptuts_options = wptuts_get_default_options();
    		  add_option( 'theme_wptuts_options', $wptuts_options );
         }
    
         // In other case we don't need to update the DB
    }
    // Initialize Theme options
    add_action( 'after_setup_theme', 'wptuts_options_init' );
    
    function wptuts_options_setup() {
    	global $pagenow;
    	if ('media-upload.php' == $pagenow || 'async-upload.php' == $pagenow) {
    		// Now we'll replace the 'Insert into Post Button inside Thickbox'
    		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 logo!', 'wptuts' );
    		}
    	}
    
    	return $translated_text;
    }
    
    // Add "WPTuts Options" link to the "Appearance" menu
    function wptuts_menu_options() {
    	//add_theme_page( $page_title, $menu_title, $capability, $menu_slug, $function);
         add_theme_page('WPTuts Options', 'WPTuts Options', 'edit_theme_options', 'wptuts-settings', 'wptuts_admin_options_page');
    }
    // Load the 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( 'WPTuts 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_logo = ! empty($input['delete_logo']) ? true : false;
    
    	if ( $submit ) {
    		if ( $wptuts_options['logo'] != $input['logo']  && $wptuts_options['logo'] != '' )
    			delete_image( $wptuts_options['logo'] );
    
    		$valid_input['logo'] = $input['logo'];
    	}
    	elseif ( $reset ) {
    		delete_image( $wptuts_options['logo'] );
    		$valid_input['logo'] = $default_options['logo'];
    	}
    	elseif ( $delete_logo ) {
    		delete_image( $wptuts_options['logo'] );
    		$valid_input['logo'] = '';
    	}
    
    	return $valid_input;
    }
    
    function delete_image( $image_url ) {
    	global $wpdb;
    
    	// We need to get the image's meta ID..
    	$query = "SELECT ID FROM wp_posts where guid = '" . esc_url($attachment_id) . "' AND post_type = 'attachment'";
    	$result = $wpdb -> get_results($query);
    
    	// And delete them (if more than one attachment is in the Library
    	foreach ( $result as $row ) {
    		wp_delete_attachment( $row -> ID );
    	}
    }
    
    /********************* JAVASCRIPT ******************************/
    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 a form section for the Logo
    	add_settings_section('wptuts_settings_header', __( 'Logo Options', 'wptuts' ), 'wptuts_settings_header_text', 'wptuts');
    
    	// Add Logo uploader
    	add_settings_field('wptuts_setting_logo',  __( 'Logo', 'wptuts' ), 'wptuts_setting_logo', 'wptuts', 'wptuts_settings_header');
    
    	// Add Current Image Preview
    	add_settings_field('wptuts_setting_logo_preview',  __( 'Logo Preview', 'wptuts' ), 'wptuts_setting_logo_preview', 'wptuts', 'wptuts_settings_header');
    }
    add_action( 'admin_init', 'wptuts_options_settings_init' );
    
    function wptuts_setting_logo_preview() {
    	$wptuts_options = get_option( 'theme_wptuts_options' );  ?>
    	<div id="upload_logo_preview" style="min-height: 100px;">
    		<img style="max-width:100%;" src="<?php echo esc_url( $wptuts_options['logo'] ); ?>" />
    	</div>
    	<?php
    }
    
    function wptuts_settings_header_text() {
    	?>
    		<p><?php _e( 'Manage Logo Options for WpTuts Theme.', 'wptuts' ); ?></p>
    	<?php
    }
    
    function wptuts_setting_logo() {
    	$wptuts_options = get_option( 'theme_wptuts_options' );
    	?>
    		<input type="hidden" id="logo_url" name="theme_wptuts_options[logo]" value="<?php echo esc_url( $wptuts_options['logo'] ); ?>" />
    		<input id="upload_logo_button" type="button" class="button" value="<?php _e( 'Upload Logo', 'wptuts' ); ?>" />
    		<?php if ( '' != $wptuts_options['logo'] ): ?>
    			<input id="delete_logo_button" name="theme_wptuts_options[delete_logo]" type="submit" class="button" value="<?php _e( 'Remove Logo', 'wptuts' ); ?>" />
    		<?php endif; ?>
    		<span class="description"><?php _e('Upload an image for the banner.', 'wptuts' ); ?></span>
    	<?php
    }
    ?>

    Pencetus Utas rikiarofiq

    (@rikiarofiq)

    Itu kan buat 1 image master…
    Kalau buat satu image saya sudah bisa …
    nah ini itu buat 2 image master…
    semua sih dah berjalan lancar…tapi ketikan di delete 1 image
    image yg ke dua itu ikut delete…

    itu yg bikin ane mumet master…mohon bantuannye…………

Melihat 2 balasan - 1 sampai 2 (dari total 2)
  • Topik ‘ASK : Theme option delete image’ tertutup untuk balasan baru.