Dealing with uploads is quite simple, actually. All you have to do is to create the input type=file bit, add the enctype to the form, and add special code to call wp_handle_upload to deal with the incoming file.
Here's a demo of a plugin with a settings page that has an upload form on it. It assumes you're uploading an image and tries to display it on the page. The file will be stored in the uploads directory, as per usual uploads. Nothing special there.
PHP Code:
<?php
/*
Plugin Name: Upload Demo
Description: Demonstrate a plugin that lets you upload an image
Author: Otto
Author URI: http://ottodestruct.com
License: GPL2
Copyright 2010 Samuel Wood (email : otto@ottodestruct.com)
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License version 2,
as published by the Free Software Foundation.
You may NOT assume that you can use any other version of the GPL.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
The license for this software can likely be found here:
http://www.gnu.org/licenses/gpl-2.0.html
*/
// add the admin page and such
add_action('admin_init', 'ud_admin_init');
function ud_admin_init() {
register_setting( 'ud_options', 'ud_options', 'ud_options_validate' );
add_settings_section('ud_main', 'Main Section', 'ud_section_text', 'ud');
add_settings_field('ud_filename', 'File:', 'ud_setting_filename', 'ud', 'ud_main');
}
// add the admin options page
add_action('admin_menu', 'ud_admin_add_page');
function ud_admin_add_page() {
$mypage = add_options_page('Upload Demo', 'Upload Demo', 'manage_options', 'ud', 'ud_options_page');
}
// display the admin options page
function ud_options_page() {
?>
<div class="wrap">
<h2>Upload Demo</h2>
<p>You can upload a file. It'll go in the uploads directory.</p>
<form method="post" enctype="multipart/form-data" action="options.php">
<?php settings_fields('ud_options'); ?>
<?php do_settings_sections('ud'); ?>
<p class="submit">
<input type="submit" name="Submit" class="button-primary" value="<?php esc_attr_e('Save Changes') ?>" />
</p>
</form>
</div>
<?php
}
function ud_section_text() {
$options = get_option('ud_options');
echo '<p>Upload your file here:</p>';
if ($file = $options['file']) {
// var_dump($file);
echo "<img src='{$file['url']}' />";
}
}
function ud_setting_filename() {
echo '<input type="file" name="ud_filename" size="40" />';
}
function ud_options_validate($input) {
$newinput = array();
if ($_FILES['ud_filename']) {
$overrides = array('test_form' => false);
$file = wp_handle_upload($_FILES['ud_filename'], $overrides);
$newinput['file'] = $file;
}
return $newinput;
}
Note that almost all of that is merely creating the settings page.
- ud_admin_init registers the setting and creates the relevant sections and fields.
- ud_admin_add_page creates the options page for the Upload Demo.
- ud_options_page displays the options page. Only thing special here is the enctype="multipart/form-data" bit, to allow file uploads to work.
- ud_section_text gets called to display the section intro. I chose to display the uploaded image here.
- ud_setting_filename is the input piece for the file browser and such.
- ud_options_validate is called to handle the resulting form submission. It checks for the presence of the file in the $_FILES superglobal, then calls wp_handle_upload to deal with it. wp_handle_upload returns an array of info about the resulting file, which we store in our options setting for later usage. The options are returned here, and WordPress handles saving them for us.
Settings pages can be really, really easy once you start using the Settings API.
Edit: Also note the "test_form" override being set to false? This is because wp_handle_upload, by default, does nothing. It ignores the file upload. You have to explicitly set test_form to false to get it to work. This is handy for development, when you don't want your file uploads actually happening while you test other pieces.