• We have a website with images that do not contain any attachment metadata. I set out to create my own function that checks if there is metadata, and if there is not any, it will create it.

    Unfortunately, I am afraid that wp_get_attachment_metadata stays empty all of the time. Or that I can’t update the metadata with wp_update_attachment_metadata

    My function is as followed:

    function custom_attach_func($attch_id) {
        $meta_data = wp_get_attachment_metadata($attch_id);
        if($meta_data == ''){
            $attachment_path = get_attached_file($attch_id);
            require_once( ABSPATH . 'wp-admin/includes/image.php' );
            $attach_data = wp_generate_attachment_metadata( $attch_id, $attachment_path);
            wp_update_attachment_metadata( $attach_id,  $attach_data );
            return $attach_data;
        }else{
            return wp_get_attachment_metadata($attch_id);
        }
    }

    While wp_generate_attachment_metadata() does generate the metadata, and wp_update_attachment_metadata returns true, wp_get_attachment_metadata() keeps returning ''.

    Is this a WordPress bug? Am I doing it wrong?!

  • The topic ‘wp_get_attachment_metadata stays empty’ is closed to new replies.