Otto, Thank you for your input.
Looks like I'm going to be quite busy the next few days. ;)
Otto, Thank you for your input.
Looks like I'm going to be quite busy the next few days. ;)
Hi Otto,
I have your method working, the only problem I see is as follows:
The upload input is one of several options on my settings page.
If I click on choose file and then select an image and then press save, everything works hunky dory as in the image is displayed. However, the file input now returns to 'No file chosen'. This means if I now update another option on this page (i.e. update another setting) then the uploaded item is now removed.
I can think of a way around it, by saving the uploaded file path as another option (called, for example "my_other_option") and if the form is updated and the file upload input is empty then we don't update "my_other_option". I was wondering if there was a cleaner way of doing this as it seems a bit over-the-top.
Probably me being tired and useless though ;)
In my example, I don't save the existing filename in the validation function if there already was one. That causes the file to be "erased" when you submit without a file upload.
That's because this was a demo, not a complete solution. It's just there to show you how to access things. If you want to not overwrite the existing filename, you'd need to preserve it yourself. Like so:
So basically, if you don't upload a file, the "else" kicks in, and the existing filename gets used. The $newinput that gets returned from the validation function will completely replace the ud_options in the options table. So anything you don't wanna lose has to be in there.PHP Code:function ud_options_validate($input) {
$options = get_option('ud_options');
$newinput = array();
if ($_FILES['ud_filename']) {
$overrides = array('test_form' => false);
$file = wp_handle_upload($_FILES['ud_filename'], $overrides);
$newinput['file'] = $file;
} else {
$newinput['file'] = $options['file'];
}
return $newinput;
}
Some people prefer to do something more like this for their validation function:
This has the effect of always preserving options and it's slightly simpler. I prefer not to do this myself, because I like to be sure that I have my own code for every case instead of letting things "fall-through", sort of thing. Keeps my code clean and well-defined.PHP Code:function options_validate($input) {
$options = get_option('ud_options');
// go through and validate each input and for each do this:
// $options['whatever'] = $input['whatever'];
return $options;
}
Michael, it looks like your .zip file you've uploaded isn't working.
I've used a desktop tool and an online tool, and both report back that the file couldn't be recognized or is supported.
Using the code you have posted, by pasting it into my functions.php file it breaks everything. I get a white screen.
Any ideas where I go wrong? :)
Otto, while playing with your validator I've found that it still didn't preserve the original upload on subsequent saves. For whatever reason, $_FILES is returning true and results in the variable storing an error ("No file was uploaded") being stored in the options variable.
Instead, I've simply altered the if statement below to check if an error results from the upload dialoge and proceed accordingly:
It tested out clean for me, but are there any pitfalls or anything I'm doing wrong?PHP Code:function ud_options_validate($input) {
$options = get_option('ud_options');
$newinput = array();
if (!$_FILES['ud_filename']['error']) {
$overrides = array('test_form' => false);
$file = wp_handle_upload($_FILES['ud_filename'], $overrides);
$newinput['file'] = $file;
} else {
$newinput['file'] = $options['file'];
}
return $newinput;
}
-Brian
I'd not be comfortable checking for the lack of error there. No file uploaded at all would have no error either.
Try checking for isset($_FILES['ud_filename']) or !empty($_FILES['ud_filename']) instead. Cleaner.
I'm not offhand familiar with what the $_FILES superglobal will look like, so play with it until you find the right way.
Actually, the problem I was running into is that no file uploaded produced an error, because the $_FILES variable is only set after POST has occurred. So, no matter what, it's storing something.
Instead, though, checking if ($_FILES[THEME_PREFIX.'admin_logo_img']['file']) makes even more sense. Not sure why I immediately jumped to "if no error" instead of "if it's identified a file" yesterday.
-Brian
Probably a little too late but here are some alternatives:
Using the WordPress Uploader in Your Plugin or Theme: http://www.webmaster-source.com/2010...ugin-or-theme/
Multiple Media Uploads in WordPress’ Functions.php (which is a tweak from the above tutorial to allow for multiple uploads) http://www.theenglishguy.co.uk/2010/...functions-php/
Hrmph. I'm running into a filesize error all of a sudden. Looked through my changelog and the file is still identical. Any thougts?
The Error:
The validation function:HTML Code:Warning: filesize() [function.filesize]: stat failed for /private/var/tmp/php5Fz2m6 in /wp-admin/includes/ms.php on line 31
PHP Code:function sb_upload_validate($input) {
$options = get_option(THEME_PREFIX.'admin_logo_img');
$newinput = array();
if ($_FILES[THEME_PREFIX.'admin_logo_img']['name']) {
$file = wp_handle_upload($_FILES[THEME_PREFIX.'admin_logo_img'], array('test_form' => false));
$newinput = $file;
} else {
$newinput = $options;
}
return $newinput;
}
-Brian
"stat failed".
Basically means your file wasn't there when it looked for it.
Run out of tmp space, maybe? Hit some other limit somewhere?