I followed the method used in the Satoshi WordPress Theme in order to let the user upload an image to a section of the page (and add some text).
Right now, the code just let me uploads one image.
Do I have to create separate files in order to archive this?
(e.g custom-slider-1.php, custom-slider-2.php, custom-slider-3.php, etc…)?
Or there is a simpler option?
Code:
upload-file.php:
<?php
if (move_uploaded_file($_FILES['uploadfile']['tmp_name'], "images/" . $_FILES["uploadfile"]["name"])) {
echo "success";
} else {
echo "error";
}
?>
custom-slider.php (it is called to be placed in a div in *functions.php):*
<?php
$option_fields[] = $slider_image = THEME_PREFIX . "slider_image";
$option_fields[] = $slider_image_enabled = THEME_PREFIX . "slider_image_enabled";
$option_fields[] = $slider_text = THEME_PREFIX . "slider_text";
?>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script>
<script type="text/javascript" src="<?php bloginfo('template_directory'); ?>/js/ajaxupload.3.5.js" ></script>
<script type="text/javascript" >
$(function(){
var btnUpload=$('#slider-upload');
var status=$('#slider-upload-status');
new AjaxUpload(btnUpload, {
action: '<?php bloginfo('template_directory'); ?>/upload-file.php',
name: 'uploadfile',
onSubmit: function(file, ext){
if (! (ext && /^(jpg|png|jpeg|gif)$/.test(ext))){
// extension is not allowed
status.text('Only JPG, PNG or GIF files are allowed');
return false;
}
status.text('Uploading...');
},
onComplete: function(file, response){
//On completion clear the status
status.text('');
//Add uploaded file to list
if(response==="success"){
$('<li></li>').appendTo('#files').html('<img src="<?php bloginfo('template_directory'); ?>/images/'+file+'" alt="" /><br />'+file).addClass('success');
$('#<?php echo $slider_image; ?>').val(file);
} else{
$('<li></li>').appendTo('#files').text(file).addClass('error');
}
}
});
});
</script>
<div class="postbox">
<h3>Slider Customization Options</h3>
<div class="postbox-content">
<div class="option-row">
<p>Choose how you would like to display your slider.</p>
</div><!--end option-row-->
<div class="option-row">
<div class="option-name">
<label>Use Image For Slider</label>
</div><!--end option-name-->
<div class="option-value">
<input class="checkbox" id="<?php echo $slider_image_enabled; ?>" type="checkbox" name="<?php echo $slider_image_enabled; ?>" value="true"<?php checked(TRUE, (bool) get_option($slider_image_enabled)); ?> />
</div><!--end option-value-->
</div><!--end option-row-->
<div class="option-row">
<div class="option-name">
<label>Upload An Image</label>
<span id="slider-upload-status"></span>
</div><!--end option-name-->
<div class="option-value">
<input class="logo-name" id="<?php echo $slider_image; ?>" type="text" name="<?php echo $slider_image; ?>" value="<?php echo get_option($slider_image); ?>" />
<input type="button" class="background_pattern_button" id="slider-upload" value="Choose Image" />
</div><!--end option-value-->
</div><!--end option-row-->
<div class="option-row">
<div class="option-name">
<label>Slider Text</label>
</div><!--end option-name-->
<div class="option-value">
<input class="input-box" id="<?php echo $slider_text; ?>" type="text" name="<?php echo $slider_text; ?>" value="<?php echo get_option($slider_text) ?>" />
</div><!--end option-value-->
</div><!--end option-row-->
<input type="submit" class="button" value="Save Changes" />
</div>
</div><!--end postbox-->
front-page.php:
<div id="slider">
<img src="<?php bloginfo('template_directory'); ?>/images/<?php echo get_option(THEME_PREFIX . 'slider_image'); ?>" />
</div>
Loop over the
$_FILES
array and add each file separately. I donât have a complete code example at hand, just an excerpt from my theme options class:handle_uploads()
is called bysave_options()
from the same class.is_allowed_mime()
guarantees that I get only file the types I need, andget_media_name()
looks for a special predefined name for the file (»Logo«, »bg-body« etc.).As you can see, it isnât that hard. Just use the build-in API, not the code from your
upload-file.php
. There are many, many edge cases which WordPress will handle better than your plainmove_uploaded_file()
.