Page 2 of 3 FirstFirst 123 LastLast
Results 11 to 20 of 30

Thread: Creating an Upload Function For an Options Page?

  1. #11
    Edwin is offline Hello World
    Join Date
    Nov 2009
    Posts
    47

    Default

    Otto, Thank you for your input.
    Looks like I'm going to be quite busy the next few days. ;)

  2. #12
    iamfriendly is offline Hello World
    Join Date
    Mar 2010
    Location
    Manchester, UK
    Posts
    11

    Default

    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 ;)

  3. #13
    Otto's Avatar
    Otto is offline On The Rocks
    Join Date
    Apr 2009
    Location
    Memphis, TN
    Posts
    862

    Default

    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:

    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;

    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.

    Some people prefer to do something more like this for their validation function:

    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;

    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.

  4. #14
    Edwin is offline Hello World
    Join Date
    Nov 2009
    Posts
    47

    Default

    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? :)

  5. #15
    rzen's Avatar
    rzen is offline Hello World
    Join Date
    May 2010
    Location
    Grandville, MI
    Posts
    13

    Default

    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:

    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;

    It tested out clean for me, but are there any pitfalls or anything I'm doing wrong?
    -Brian

  6. #16
    Otto's Avatar
    Otto is offline On The Rocks
    Join Date
    Apr 2009
    Location
    Memphis, TN
    Posts
    862

    Default

    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.

  7. #17
    rzen's Avatar
    rzen is offline Hello World
    Join Date
    May 2010
    Location
    Grandville, MI
    Posts
    13

    Default

    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

  8. #18
    hafiz is offline Hello World
    Join Date
    Mar 2009
    Posts
    11

    Default

    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/

  9. #19
    rzen's Avatar
    rzen is offline Hello World
    Join Date
    May 2010
    Location
    Grandville, MI
    Posts
    13

    Default

    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:
    HTML Code:
    Warning: filesize() [function.filesize]: stat failed for /private/var/tmp/php5Fz2m6 in /wp-admin/includes/ms.php on line 31
    The validation function:
    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

  10. #20
    Otto's Avatar
    Otto is offline On The Rocks
    Join Date
    Apr 2009
    Location
    Memphis, TN
    Posts
    862

    Default

    "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?

Page 2 of 3 FirstFirst 123 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •