WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Is there an easy way to include an upload box to your settings page?

I am building an Open Graph options page and I like users to upload a standard image directly from that page.

share|improve this question
This was more what I wanted: webmaster-source.com/2010/01/08/… Now to figure out how you can detect if a certain image has been deleted. – Mark Feb 9 '12 at 16:08
up vote 2 down vote accepted

WordPress provides a convenient function for just this purpose: wp_handle_upload().

Assuming that you already have the appropriate file form field in your settings page, and that you're using register_setting() for your options, and therefore already have an options validation callback, simply handle the file form field data using wp_handle_upload(). Here's an example:

// Validate file fields
else if ( 'file' == $optiondetails['type'] ) {
    if ( isset( $input[$setting] ) ) {
        // Only update setting if input value is in the list of valid options
        $setting_file = $setting . '_file';
        $valid_input[$setting] = ( isset( $_FILES[$setting_file] ) ? theme-slug_image_upload( $setting, $input ) : $valid_input[$setting] );

Then, you just need to define that theme-slug_image_upload() callback, using wp_handle_upload():

function theme-slug_image_upload( $the_file, $input ) {
    $data = $_FILES[$the_file . '_file'];
    if ( '' != $data['name'] )
        $upload = wp_handle_upload( $_FILES[$the_file . '_file'], array( 'test_form' => false ) );
        $upload['url'] = $input[$the_file];
    return $upload['url'];

That's pretty much it.

share|improve this answer
It would be so nice with a full example of the validation callback functions. I can't seem to make it work. – windyjonas Apr 22 '13 at 15:04

protected by Community May 2 '14 at 15:45

Thank you for your interest in this question. Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).

Would you like to answer one of these unanswered questions instead?

Not the answer you're looking for? Browse other questions tagged or ask your own question.