Since the release of the latest WooThemes’ extension, WooCommerce Photography, a lot of photographers asked me how to print EXIF data on the photograph page.
This feature is not included in WooCommerce Photography. I did some research about what exactly EXIF data is (I’m not a photographer at all) and if is possible to get them with PHP and turns out that it’s possible, and easy.
With this tutorial you will learn how to add a custom tab in the single product page to show EXIF data.
Since I’m not a photographer I’m not really sure about what are the EXIF data that a photographer might want to show to his customer.
I inserted in the code, by default, those that seems to be the most important to me. Feel free to edit the code and add more data if needed.
But now let’s start.
Open your functions.php located in wp-content/themes/your-theme-name/ and add this code at the end of it:
This is all you need to print EXIF data.
In details, the first function,
wc_photography_exif_tab, add a new tab in WooCommerce for all your photographs. If the product looped is not a photograph, the tab will not be added.
The second function instead, is the one that cares about getting and printing the EXIF data. Is the core of this tutorial.
As you can see from the code on line 32, there’s an array with the allowed extensions. This is because, EXIF data exist only in JPG, JPEG and TIFF photos. Since TIFF is not an allowed mime type in WordPress, i allowed only JPG and JPEG.
The code checks the featured image extension, with this array, and if allowed, will go ahead retrieving the EXIF data on line 37.
Also here, there’s an array with all the EXIF data we want to print in the single product page. As I said, I’m not a photographer, so I added in the list those who makes more sense to me. You can check the complete list of data available adding this code after line 37:
echo '<pre>'; var_dump( $exif ); echo '</pre>';
It will print a list of data in the tab on the single product page. Grab what you need and add them in the $allowed_exif array using the same format.
After that, the foreach loop just check if the looped EXIF data is allowed or not, and if yes, print it in the table.
This is how it should look in the single product page, using the theme TwentyTwelve:
I hope you find this tutorial useful for your business and your site. If you think that I missed something please feel free to comment!
P.S. Many thanks to Sven for sending me the photo I used to test, the one you can see in this screenshot above!
Leave a Reply