0

So I'm still building the settings page for my theme and I'm busy with the file upload function. The wp_handle_upload function is working but the file is not being grabbed.

This is the registered option:

add_settings_field("upload_logo", "Upload Logo", "logo_display", "theme-options", "section");
register_setting("section", "upload_logo", "handle_logo_upload");

This is the function that set's up the theme page:

function theme_settings_page() {
    ?>
        <div class="wrap">
        <h1>Theme Panel</h1>
        <form method="post" action="options.php" enctype="multipart/form-data">
            <?php
                settings_fields("section");
                do_settings_sections("theme-options");      
                submit_button(); 
            ?>          
        </form>
        </div>
    <?php
}

This is the function that accepts an image:

function logo_display()
{
    ?>
     <form method="post" action="options.php" enctype="multipart/form-data">
        <input type="file" name="upload_logo" id="upload_logo" value="<?php echo get_option('upload_logo'); ?>"/>         
    </form>
   <?php
}

This is the function that handles the upload:

function handle_logo_upload() {
    if ( !function_exists( 'wp_handle_upload' ) ) {
        require_once( ABSPATH . 'wp-admin/includes/file.php' );
    }
        $uploadedfile = $_FILES['upload_logo']['submit'];
        $upload_overrides = array( 'test_form' => false );
        $movefile = wp_handle_upload( $uploadedfile, $upload_overrides );
        if ( $movefile ) {
            $wp_filetype = $movefile['type'];
            $filename = $movefile['file'];
            $wp_upload_dir = wp_upload_dir();
            $attachment = array(
                'guid' => $wp_upload_dir['url'] . '/' . basename( $filename ),
                'post_mime_type' => $wp_filetype,
                'post_title' => preg_replace('/\.[^.]+$/', '', basename($filename)),
                'post_content' => '',
                'post_status' => 'inherit'
            );
            $attach_id = wp_insert_attachment( $attachment, $filename);
            echo '<br>';
            //return $attach_id;
            var_dump($uploadedfile);
            //print_r($uploadedfile);
            //wp_die('end');
        }

    return 'fail';      
}

When I do I var_dump on the variable $uploadedfile, I get NULL. Why is that?

Here is a screenshot of the settings page for my theme: http://pasteboard.co/yFck7LW.png

This is the empty file that is uploaded when I try to upload something: http://pasteboard.co/yFhrUbB.png

Please help!

1
  • Try var_dumping $_FILES, without any array index after it, and see what you get. Commented Sep 11, 2015 at 11:03

1 Answer 1

2

Here's the problem:

$uploadedfile = $_FILES['upload_logo']['submit'];

change that to:

$uploadedfile = $_FILES['upload_logo']['name'];
Sign up to request clarification or add additional context in comments.

2 Comments

Wow, that worked! $uploadedfile is not returning NULL anymore but name of image. You are wonder master !!
@emporio, if it worked, try to mark it as the accepted answer, so others facing similar issues can find the help they need.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.