Add value to WP Attachment Meta Data

I would like to let WordPress calculate the aspect ratio & orientation when the media is uploaded instead of doing this in the templates. Then I can call images and ask for image.orientation which will give me ‘portrait’ or ‘landscape’ as a value.

Example, how it would look like after uploading an image:
Example, how it would look like after uploading an image

Read More

Is this possible or do I have to do this in the frontend?

Related posts

Leave a Reply

1 comment

  1. You can write some code for it. Use http://codex.wordpress.org/Function_Reference/wp_get_attachment_metadata to get the width and the height, and then if $width/$height > 1 => landscape, otherwise its portrait. Store this value in the attachment using: http://codex.wordpress.org/Function_Reference/wp_update_attachment_metadata.

    You can use an action for this, such as:

    add_action('add_attachment', 'process_images');
    

    Adding the solution as well, following from your code attempt (I HAVENT TESTED THIS):

    function attachment_orientation_meta($id){
        $attachmentInfo = wp_get_attachment_metadata($id);
        $attachmentOrientation = ( $attachmentInfo['height'] > $attachmentInfo['width'] ) ? 'portrait' : 'landscape';
        update_post_meta($id, 'orientation', $attachmentOrientation);
    }
    add_action( 'add_attachment', 'attachment_orientation_meta' );