[php] Upload Option für WordPress Theme-Page

L.B.

Ensign
Registriert
Juni 2010
Beiträge
209
Hallo Zusammen,

ich entwickle seit geraumer Zeit ein WordPress-Theme und versuche mich momentan an einer Upload-Funktion für Bilder innerhalb des Option-Panels. In diesem Panel werden alle Einstellungen, die das Theme betreffen, vorgenommen.

Der zentrale Code für das Options-Panel sieht folgendermaßen aus:

PHP:
<?php
/*
*
*	theme_functions.php
*
*	LB-Projects Theme
*	Autor: Lukas Bommes
*	Copyright (C) Lukas Bommes
*
*/

function LB_add_init() {
	wp_enqueue_style("theme_functions", get_template_directory_uri() . "/admin/theme_functions.css");
	wp_enqueue_script("theme_functions", get_template_directory_uri() . "/admin/theme_functions.js");
	wp_enqueue_style( 'farbtastic' );
    wp_enqueue_script( 'farbtastic' );	
}

//Funktionen für das Admin-Panel
function LB_add_admin() {
      
	global $themename, $shortname, $options;
		  
	if( isset( $_GET['page'] ) == basename(__FILE__) ) {
		  
		if( isset( $_REQUEST['action'] ) && $_REQUEST['action'] == 'save' ) {
		  
			foreach ($options as $value) {
				if( isset( $value['id'] ) && isset( $_REQUEST[ $value['id'] ] ) ) {
					update_option( $value['id'], $_REQUEST[ $value['id'] ] );
				}
			}  
		  
			foreach ($options as $value) {  
				if( isset( $value['id'] ) && isset( $_REQUEST[ $value['id'] ] ) ) {
					update_option( $value['id'], $_REQUEST[ $value['id'] ] ); 
				} 
				else if( isset( $value['id'] ) ) { 
					delete_option( $value['id'] ); 
				} 
			}  
		  
			header("Location: admin.php?page=theme_functions.php&saved=true");  
			die;  
		  
		}  
		
		else if( isset( $_REQUEST['action'] ) && $_REQUEST['action'] == 'reset' ) { 
		  
			foreach ($options as $value) {
				if( isset( $value['id'] ) ) {			
					delete_option( $value['id'] );
				}
			}  
		  
			header("Location: admin.php?page=theme_functions.php&reset=true");  
			die;  
		  
		}  
	}

	add_theme_page( __('Theme Options', 'lbprojects'), __('Theme Options', 'lbprojects'), 'administrator', basename(__FILE__), 'LB_admin');  
}

function LB_admin() {
      
	global $themename, $shortname, $options;  
	$i=0;
      
	if( isset( $_REQUEST['saved'] ) ) echo '<div id="message" class="updated fade"><p><strong>' . $themename . ' ' . __('settings saved.', 'lbprojects') . '</strong></p></div>';  
	if( isset( $_REQUEST['reset'] ) ) echo '<div id="message" class="updated fade"><p><strong>' . $themename . ' ' . __('settings reset.', 'lbprojects') . '</strong></p></div>';  
      
	?>
	<div class="wrap rm_wrap">
	
		<div class="icon32" id="icon-tools"></div>
		
		<h2><?php echo $themename . ' ' . __('Settings', 'lbprojects'); ?></h2>   
		  
		<div class="rm_opts">
		
			<form method="post">
			
				<?php foreach ($options as $value) {
				
					switch ( $value['type'] ) {  
					  
					case "open": ?>
					
					<?php break;
					  
					case "close": ?>
					
						</div><!-- rm_opts -->
					
						</div><!-- wrap rm_wrap -->
			
						<br />
			  
					<?php break;
			  
					case "title": ?>
					
						<p><?php _e('Below options allow easy configuration of the', 'lbprojects'); ?> <?php echo $themename;?> <?php _e('Theme.', 'lbprojects'); ?></p>  
					  
					<?php break;
					  
					case "text": ?>  
					  
						<div class="rm_input rm_text">  
							<label for="<?php echo $value['id']; ?>"><?php echo $value['name']; ?></label>  
							<input name="<?php echo $value['id']; ?>" id="<?php echo $value['id']; ?>" type="<?php echo $value['type']; ?>" value="<?php echo stripslashes( get_option( $value['id'] ) ); ?>" />
						<small><?php echo $value['desc']; ?></small><div class="clearfix"></div>  
						  
						</div> 
						
					<?php break;   
					  
					case "textarea": ?>  
					  
						<div class="rm_input rm_textarea">  
							<label for="<?php echo $value['id']; ?>"><?php echo $value['name']; ?></label>  
							<textarea name="<?php echo $value['id']; ?>" type="<?php echo $value['type']; ?>" cols="" rows=""><?php echo stripslashes( get_option( $value['id'] ) ); ?></textarea>  
							<small><?php echo $value['desc']; ?></small><div class="clearfix"></div>  
						</div>  
					  
					<?php break;
					  
					case "select": ?>  
					  
						<div class="rm_input rm_select"> 
							<label for="<?php echo $value['id']; ?>"><?php echo $value['name']; ?></label>
							<select name="<?php echo $value['id']; ?>" id="<?php echo $value['id']; ?>">  
							<?php foreach ($value['options'] as $option) { ?>  
									<option <?php if(get_option( $value['id'] ) == $option) { echo 'selected="selected"'; } ?>><?php echo $option; ?></option><?php } ?>  
							</select>
							<small><?php echo $value['desc']; ?></small><div class="clearfix"></div>  
						</div>
						
					<?php break;
					  
					case "checkbox": ?>  
					  
						<div class="rm_input rm_checkbox">  
							<label for="<?php echo $value['id']; ?>"><?php echo $value['name']; ?></label> 
							<?php if(get_option($value['id'])){ $checked = "checked=\"checked\""; }else{ $checked = "";} ?>  
							<input type="checkbox" name="<?php echo $value['id']; ?>" id="<?php echo $value['id']; ?>" value="true" <?php echo $checked; ?> />  
							<small><?php echo $value['desc']; ?></small><div class="clearfix"></div>  
						</div>
						
					<?php break;
					
					case "colorpicker":
					
						$i++; ?>
					  
						<script type="text/javascript">
							jQuery(document).ready(function($){
								$('#color_picker_color<?php echo $i; ?>').farbtastic('#<?php echo $value['id']; ?>');	
							});
						</script>
						
						<div class="rm_input rm_colorpicker">
							<label for="<?php echo $value['id']; ?>"><?php echo $value['name']; ?></label>
							<input type="text" id="<?php echo $value['id']; ?>" name="<?php echo $value['id']; ?>" value="<?php if( get_option( $value['id'] ) != "") { echo get_option($value['id']); } else { echo '#ffffff'; } ?>" />
							<small><?php echo $value['desc']; ?></small><div class="clearfix"></div>
							<div id="color_picker_color<?php echo $i; ?>" class="rm_color-picker"></div>
						</div>
						
					<?php break;

					case "section": ?>
					  
						<div class="rm_section">				
							<div class="rm_title">
								<h3><span class="inactive"></span><?php echo $value['name']; ?></h3>
								<div class="clearfix"></div>
							</div>
							<div class="rm_options">
					  
					<?php break;
					  
					}
				} ?>
				
				<span class="submit"><input id="save-button" name="save" type="submit" value="<?php _e('Save changes', 'lbprojects'); ?>" /></span>
				<input type="hidden" name="action" value="save" />
				
			</form>
			
			<form method="post">
			
				<span class="submit"><input name="reset" type="submit" value="<?php _e('Reset', 'lbprojects'); ?>" /></span>  
				<input type="hidden" name="action" value="reset" />
				
			</form>
			
		</div><!-- rm_options -->
      
<?php  
}

//Initialisieren
add_action('admin_init', 'LB_add_init');
add_action('admin_menu', 'LB_add_admin');
?>

Es handelt sich dabei um eine optimierte Variante dieses Panels.

Wie man sehen kann, bietet das Panel schon Optionen für Text, Textboxen, Checkboxen und Farbboxen an, nur die Upload-Funktion fehlt noch.

Diese habe ich nach dieser Anleitung hinzugefügt, sodass der Code nun folgendermaßen aussieht:

PHP:
<?php
/*
*
*	theme_functions.php
*
*	LB-Projects Theme
*	Autor: Lukas Bommes
*	Copyright (C) Lukas Bommes
*
*/

function LB_add_init() {
	wp_enqueue_style("theme_functions", get_template_directory_uri() . "/admin/theme_functions.css");
	wp_enqueue_script("theme_functions", get_template_directory_uri() . "/admin/theme_functions.js");
}

//Funktionen für das Admin-Panel
function LB_add_admin() {
      
	global $themename, $shortname, $options;
		  
	if( isset( $_GET['page'] ) == basename(__FILE__) ) {
		  
		if( isset( $_REQUEST['action'] ) && $_REQUEST['action'] == 'save' ) {
		  
			foreach ($options as $value) {
				if( isset( $value['id'] ) && isset( $_REQUEST[ $value['id'] ] ) ) {
					update_option( $value['id'], $_REQUEST[ $value['id'] ] );
				}
			}  
		  
			foreach ($options as $value) {  

				if( $value['type'] == 'upload' ) {				
					$upload = wp_handle_upload( $_FILES[ $value['id'] ] );					
					if( isset( $upload['url'] ) ) {
						update_option( $value['id'], $upload['url'] );
					}					
				}
				elseif( isset( $_REQUEST[ $value['id'] ] ) ) {
					update_option( $value['id'], $_REQUEST[ $value['id'] ]  );
				}

			}  
		  
			header("Location: admin.php?page=theme_functions.php&saved=true");  
			die;  
		  
		}  
		
		else if( isset( $_REQUEST['action'] ) && $_REQUEST['action'] == 'reset' ) { 
		  
			foreach ($options as $value) {
				if( isset( $value['id'] ) ) {			
					delete_option( $value['id'] );
				}
			}  
		  
			header("Location: admin.php?page=theme_functions.php&reset=true");  
			die;  
		  
		}  
	}

	add_theme_page( __('Theme Options', 'lbprojects'), __('Theme Options', 'lbprojects'), 'administrator', basename(__FILE__), 'LB_admin');  
}

function LB_admin() {
      
	global $themename, $shortname, $options;  
	$i=0;
      
	if( isset( $_REQUEST['saved'] ) ) echo '<div id="message" class="updated fade"><p><strong>' . $themename . ' ' . __('settings saved.', 'lbprojects') . '</strong></p></div>';  
	if( isset( $_REQUEST['reset'] ) ) echo '<div id="message" class="updated fade"><p><strong>' . $themename . ' ' . __('settings reset.', 'lbprojects') . '</strong></p></div>';  
      
	?>
	<div class="wrap rm_wrap">
	
		<div class="icon32" id="icon-tools"></div>
		
		<h2><?php echo $themename . ' ' . __('Settings', 'lbprojects'); ?></h2>   
		  
		<div class="rm_opts">
		
			<form method="post" enctype="multipart/form-data" >
			
				<?php foreach ($options as $value) {
				
					switch ( $value['type'] ) {  
					  
					case "open": ?>
					
					<?php break;
					  
					case "close": ?>
					
						</div><!-- rm_opts -->
					
						</div><!-- wrap rm_wrap -->
			
						<br />
			  
					<?php break;
			  
					case "title": ?>
					
						<p><?php _e('Below options allow easy configuration of the', 'lbprojects'); ?> <?php echo $themename;?> <?php _e('Theme.', 'lbprojects'); ?></p>  
					  
					<?php break;
					  
					case 'text': ?>  
					  
						<div class="rm_input rm_text">  
							<label for="<?php echo $value['id']; ?>"><?php echo $value['name']; ?></label>  
							<input name="<?php echo $value['id']; ?>" id="<?php echo $value['id']; ?>" type="<?php echo $value['type']; ?>" value="<?php echo stripslashes( get_option( $value['id'] ) ); ?>" />
						<small><?php echo $value['desc']; ?></small><div class="clearfix"></div>  
						  
						</div> 
						
					<?php break;   
					  
					case 'textarea': ?>  
					  
						<div class="rm_input rm_textarea">  
							<label for="<?php echo $value['id']; ?>"><?php echo $value['name']; ?></label>  
							<textarea name="<?php echo $value['id']; ?>" type="<?php echo $value['type']; ?>" cols="" rows=""><?php echo stripslashes( get_option( $value['id'] ) ); ?></textarea>  
							<small><?php echo $value['desc']; ?></small><div class="clearfix"></div>  
						</div>  
					  
					<?php break;
					  
					case 'select': ?>  
					  
						<div class="rm_input rm_select"> 
							<label for="<?php echo $value['id']; ?>"><?php echo $value['name']; ?></label>
							<select name="<?php echo $value['id']; ?>" id="<?php echo $value['id']; ?>">  
							<?php foreach ($value['options'] as $option) { ?>  
									<option <?php if(get_option( $value['id'] ) == $option) { echo 'selected="selected"'; } ?>><?php echo $option; ?></option><?php } ?>  
							</select>
							<small><?php echo $value['desc']; ?></small><div class="clearfix"></div>  
						</div>
						
					<?php break;
					  
					case "checkbox": ?>  
					  
						<div class="rm_input rm_checkbox">  
							<label for="<?php echo $value['id']; ?>"><?php echo $value['name']; ?></label> 
							<?php if(get_option($value['id'])){ $checked = "checked=\"checked\""; }else{ $checked = "";} ?>  
							<input type="checkbox" name="<?php echo $value['id']; ?>" id="<?php echo $value['id']; ?>" value="true" <?php echo $checked; ?> />  
							<small><?php echo $value['desc']; ?></small><div class="clearfix"></div>  
						</div>
						
					<?php break;
					
					case "colorpicker":
					
						$i++; ?>
					  
						<script type="text/javascript">
							jQuery(document).ready(function($){
								$('#color_picker_color<?php echo $i; ?>').farbtastic('#<?php echo $value['id']; ?>');	
							});
						</script>
						
						<div class="rm_input rm_colorpicker">
							<label for="<?php echo $value['id']; ?>"><?php echo $value['name']; ?></label>
							<input type="text" id="<?php echo $value['id']; ?>" name="<?php echo $value['id']; ?>" value="<?php if( get_option( $value['id'] ) != "") { echo get_option($value['id']); } else { echo '#ffffff'; } ?>" />
							<small><?php echo $value['desc']; ?></small><div class="clearfix"></div>
							<div id="color_picker_color<?php echo $i; ?>" class="rm_color-picker"></div>
						</div>
						
					<?php break;
					
					case "upload":	?>			
					
					<div class="rm_input rm_upload">
						<?php if($img = get_option($value['id'])){ echo "<img src='" . $img . "' />"; } ?>
						<label for="<?php echo $value['id']; ?>"><?php echo $value['name']; ?></label>
						<input type="file" name="<?php echo $value['id'] ?>" size="40" />
						<small><?php echo $value['desc']; ?></small><div class="clearfix"></div>
					</div>
					
					<?php break;

					case "section": ?>
					  
						<div class="rm_section">				
							<div class="rm_title">
								<h3><span class="inactive"></span><?php echo $value['name']; ?></h3>
								<div class="clearfix"></div>
							</div>
							<div class="rm_options">
					  
					<?php break;
					  
					}
				} ?>
				
				<span class="submit"><input id="save-button" name="save" type="submit" value="<?php _e('Save changes', 'lbprojects'); ?>" /></span>
				<input type="hidden" name="action" value="save" />
				
			</form>
			
			<form method="post">
			
				<span class="submit"><input name="reset" type="submit" value="<?php _e('Reset', 'lbprojects'); ?>" /></span>  
				<input type="hidden" name="action" value="reset" />
				
			</form>
			
		</div><!-- rm_options -->
      
<?php  
}

//Initialisieren
add_action('admin_init', 'LB_add_init');
add_action('admin_menu', 'LB_add_admin');
?>

Wie man sehen kann, ist noch der Fall "upload" zur Switch-Anweisung hinzugekommen, in der sich ein File-Input befindet. Weiterhin wurden noch einige Argumente zum Form-Tag hinzugefügt und der Handler für das Speichern der URL wurde auch angepasst.

Soweit so gut, nur funktioniert es nicht. (Was auch sonst? :D) Zwar wird das Input-Elemt angezeigt und ein Wert lässt sich auch eingeben, dieser wird aber nicht gespeichert. Wenn ich irgendwo den gespeicherten URL-Wert ausgeben will, zeigt sich, dass die URL nicht gespeichert wurde. Daher tippe ich auf den ersten Teil der Funktion LB_add_admin() als Fehlerursache.

Hat jemand eine Idee, worin das Problem bestehen könnte? Ich bin mit meinem php am Ende und Try and Error wird irgenwann auch langweilig...:freak:

Danke schonmal.
L.B.
 

Ähnliche Themen

Zurück
Oben