wp_handle_upload

Home » Functions » wp_handle_upload

Function Name: wp_handle_upload

Explanation: The wp_handle_upload function in WordPress is a powerful utility that handles the uploading of files to the WordPress media library. It provides a secure and efficient way to handle file uploads, ensuring that the files are properly processed and stored.

This function is primarily used within WordPress themes and plugins to handle file uploads from users. It takes care of various aspects of the upload process, such as validating the file type, handling file name conflicts, and moving the uploaded file to the appropriate directory within the media library.

The wp_handle_upload function returns an array containing the file details after it has been successfully processed. These details include the file’s path, URL, type, and size, among others. This allows developers to easily access and utilize the uploaded file in their WordPress applications.

Example Usage: Let’s say you’re working on a custom WordPress plugin that allows users to upload profile pictures. You can utilize the wp_handle_upload function to handle the file upload process. Here’s an example of how you can use this function:

// Assuming the file input field has the name 'profile_picture'
if (isset($_FILES['profile_picture'])) {
    $uploaded_file = $_FILES['profile_picture'];

    // Allow only image files
    $allowed_types = array('image/jpeg', 'image/png');
    $upload_overrides = array('test_form' => false);

    // Process the file upload
    $upload_result = wp_handle_upload($uploaded_file, $upload_overrides, $time = current_time('mysql'), $allowed_types);

    // Check if the upload was successful
    if ($upload_result && !isset($upload_result['error'])) {
        // Access the uploaded file details
        $file_path = $upload_result['file'];
        $file_url = $upload_result['url'];
        $file_type = $upload_result['type'];
        $file_size = $upload_result['size'];

        // Perform further actions with the uploaded file
        // e.g., save the file details in the WordPress database
        // or display the uploaded image on the user's profile page
    } else {
        // Handle any errors that occur during the upload process
        $error_message = isset($upload_result['error']) ? $upload_result['error'] : 'Unknown error occurred during file upload.';
        echo 'Error: ' . $error_message;
    }
}

In this example, we first check if the ‘profile_picture’ file input field is set. If it is, we retrieve the uploaded file details and define the allowed file types. We then call the wp_handle_upload function, passing in the uploaded file, upload overrides, current time, and allowed types as parameters. The function processes the file upload and returns an array containing the file details.

We check if the upload was successful by ensuring that the $upload_result array does not contain any error messages. If the upload was successful, we can access the file details from the $upload_result array, such as the file path, URL, type, and size. From there, we can perform further actions with the uploaded file, such as saving it in the WordPress database or displaying it on the user’s profile page.

If any errors occur during the upload process, we handle them by checking if the $upload_result array contains an error message. If it does, we display the error message to the user.

In conclusion, the wp_handle_upload function is a crucial tool for handling file uploads in WordPress, providing developers with a convenient and secure way to process and utilize uploaded files within their themes and plugins.

Learn More on WordPress.org

WordPress snippets using the wp_handle_upload function

Register an account to save your snippets or go Pro to get more features.