0

A while back I asked this question: Save camera info as metadata on image upload?

Long story short, it works in that the custom field is saved into wp_postmeta and I can access that value. But as I'm revisiting the project, I've noticed a side-effect.

With the following code in place to save the extra photo metadata, _wp_attachment_metadata no longer gets saved to the database. This obviously leads to several issues with image sizes and the image editor in WP:

add_filter( 'wp_generate_attachment_metadata', 'mvt_save_photo_credit', 10, 2 );

function mvt_save_photo_credit( $metadata, $attachment_id ) {
    add_post_meta($attachment_id, '_mvt_credit', $metadata['image_meta']['credit'], true);
}

I can't figure out why _wp_attachment_metadata no longer gets saved with this filter in place. Everything seems to line up as it should, and the filter itself successfully writes the data it's meant to. Removing the filter returns the expected functionality (_wp_attachement_metadata is correctly saved to the DB), so I know the issue has to do with this filter.

Any help would be greatly appreciated.

Thanks.

1

Given that that is a filter, it should be returning $metadata instead of returning nothing.

add_filter( 'wp_generate_attachment_metadata', 'mvt_save_photo_credit', 10, 2 );
function mvt_save_photo_credit( $metadata, $attachment_id ) {
    add_post_meta($attachment_id, '_mvt_credit', $metadata['image_meta']['credit'], true);
    return $metadata; // <-- giving back what we got
}

I was able to duplicate the issue you describe, and that small change fixed it.

This only matter for filters down line of this one, but with filters you should (nearly) always return information. You never know what will break if you don't.

  • This did the trick. Thank you for your help and insight to the "whys?" – edeneye Jun 3 '13 at 17:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.