Search code examples
wordpressmetadataattachmentexif

How To Properly Generate and Update Metadata Image Information In Wordpress


I am using a plugin to extract EXIF image data from images uploaded using a form on my wordpress site. As of now it properly uploads and inserts the image into the media library, but I dont think it is generating any metadata information and updating it. I know this because it used to work with another method I was using, but after I upgraded some plugins,it all stopped working and I can't seem to fix it... What is wrong below that might not be allowing the metadata info to generate from the uploaded image? I get this error on the attachment page:

Notice: Undefined variable: metadata in /mnt/soco-app/forms/wp-content/themes/twentytwelve/image.php on line 25

//Add uploaded image to media library
add_action("gform_after_submission", "post_submission", 10, 2);

function post_submission($entry) {

    if($_FILES['input_5']) {/**TURN WP_DEBUG OFF WHEN FIXED**/

        $filename = $entry[5];
        $wp_filetype = wp_check_filetype(basename($filename), null );
        $wp_upload_dir = wp_upload_dir();
        $attachment = array(
            'guid' => $wp_upload_dir['url'] . '/' . basename( $filename ), 
            'post_mime_type' => $wp_filetype['type'],
            'post_title' => preg_replace('/\.[^.]+$/', '', basename($filename)),
            'post_content' => '',
            'post_status' => 'inherit'
        );

        $parent_post_id = 9; //ID of Parent Page

        $attach_id = wp_insert_attachment( $attachment, $filename, $parent_post_id );

        require_once(ABSPATH . 'wp-admin/includes/image.php');

        $attach_data = wp_generate_attachment_metadata( $attach_id, $filename   );

        //require_once(ABSPATH . 'wp-includes/post.php');
        //update_post_meta( $parent_post_id, '_wp_attachment_metadata', $attach_data );
        wp_update_attachment_metadata( $attach_id,  $attach_data );
    } 
}

Solution

  • Use the wp_generate_attachment_metadata function.

    This function generates metadata for an image attachment. It also creates a thumbnail and other intermediate sizes of the image attachment based on the sizes defined on the Settings_Media_Screen.

    http://codex.wordpress.org/Function_Reference/wp_generate_attachment_metadata