WordPress file is not uploading in theme settings

So I’m building my own WordPress theme and I included a setting to be able to change the sites logo.

This is the part that registers the function that handles the upload:

Read More
add_settings_field("logo", "Logo", "logo_display", "theme-options", "section");
register_setting("section", "logo", "handle_logo_upload");

This is the handler function that is supposed to handle the file given:

function handle_logo_upload()
{
    if ( ! function_exists( 'wp_handle_upload' ) ) {
        require_once( ABSPATH . 'wp-admin/includes/file.php' );
        }

        $uploadedfile = $_FILES['file'];

        $upload_overrides = array( 'test_form' => false );

        $movefile = wp_handle_upload( $uploadedfile, $upload_overrides );

        if ( $movefile && !isset( $movefile['error'] ) ) {
            echo "File is valid, and was successfully uploaded.n";
            var_dump( $movefile);
        } else {
            /**
             * Error generated by _wp_handle_upload()
             * @see _wp_handle_upload() in wp-admin/includes/file.php
             */
        echo $movefile['error'];
    }

    return 'stringaz';      
}

This is what the theme panel looks like: http://pasteboard.co/uwC3Qmi.png

Here you can see that the handler is not working because a string called ‘stringaz’ is being presented where the logo should appear: http://pasteboard.co/vsnqkay.png

What do I need to change to the handler function to get this code to run?

cheers!

Related posts

2 comments

  1. you can try this one, its example, you should change your own variable name and all that:

    if(!empty($_FILES['user_image']["name"]))
    {
        if( $_FILES['user_image']['type'] == "image/jpeg" || $_FILES['user_image']['type'] == "image/png" || $_FILES['user_image']['type'] == "image/jpg")
        {
            $wp_upload_dir = wp_upload_dir();
            $target_file = $wp_upload_dir['path']."/".$_FILES['user_image']["name"];
            $target_file1 = $wp_upload_dir['url']."/".$_FILES['user_image']["name"];
            if ( move_uploaded_file($_FILES['user_image']["tmp_name"], $target_file) )
                update_user_meta($current_user->ID, 'cupp_upload_meta', $target_file1 );
            else
                $errmsg = "Sorry, there was an error uploading your file.";
        }
        else
        {
            $errmsg = "Image file have not valid extension.";
        }
    }
    
  2. Add enctype=”multipart/form-data” in <form> tag.
    Like <form method="post" action="options.php" enctype="multipart/form-data">

    function handle_logo_upload()
    {
        if(!function_exists('wp_handle_upload')){
            require_once(ABSPATH.'wp-admin/includes/file.php');
        }
        if(!empty($_FILES["logo"]["tmp_name"]))
        {
            $urls = wp_handle_upload($_FILES["logo"], array('test_form' => FALSE));
            $temp = $urls["url"];
            return $temp;   
        }
        return $option;
    }
    

    It’s Done! 🙂

Comments are closed.