Support

Account

Home Forums Feedback Auto Prefill Fields based on EXIF

Solving

Auto Prefill Fields based on EXIF

  • Hi folks,

    I’m actually adding multiple custom fields (attachments only) to handle specific metadata for my photography activity.
    For instance, for each of my photos, I need to be able to set specials attributes like Version Name, Category, City, State/Province, Country, Max Available Width/Height, etc.
    Adding those customs fields isn’t a problem at all.

    What I would like is those fields to be prefilled when I’m uploading a new photo. Each of my photo contains these metadata (as EXIF or IPTC etc.), so it could be extremely convenient to automate the process. I would just have to check them out rather than manually filling all of them (huge work).

    Could you provide me some guidance on how to code a custom function that will trigger after an image upload is done, to extract those metadata from my photo and prefill my customs fields?

    Thanks in advance.

  • This is actually what I’ve done so far, but it only works when afc/save_post triggers, and I’ve no live refresh of the fields either:

    
    if (class_exists('acf')){
    	function taz_prefill_metadata($post_id) {
    		$fullImagePath = wp_get_attachment_url($post_id);
    		$metadata = exif_read_data($fullImagePath);
    		if ($metadata === false) {
    			exit;
    		} else {
    			$customFields = get_field_objects($post_id);
    			update_field($customFields['reference']['name'], (string)$metadata[FocalLength], $post_id);
    		}
    	}
    }
    add_action('acf/save_post', 'taz_prefill_metadata', 20); // run after ACF saves the $_POST['acf'] data
    
  • I’m not sure if this is going to be any help to you or not because I don’t know exactly how to accomplish what you want to do.

    If you want to see your additional fields refreshed when an image is uploaded and chosen, you’ll need to do some of your own AJAX to get it done. I know that image uploads are handled by WP and not by ACF. After the upload an event is triggered that causes ACF to get the data related to the image and populate the image field.

    Your best bet is to add an event action when this happens, do your own AJAX request to get the additional information that you are looking for and then populate the additional fields. This is where my knowledge is lacking, but I can give you some examples of triggering AJAX requests when ACF fields change and populating other fields based on the results of the requests. There just isn’t anything specifically related to doing this for an image field so you’ll need to do some investigation to get it done and it will likely take a bit of trial and error. Anyway, you can find my examples of dynamically loading ACF fields using AJAX here https://github.com/Hube2/acf-dynamic-ajax-select-example

  • Hey John, sorry for my late answer and thanks for yours.
    I’ve was indeed expecting having to code an AJAX request on my own.
    I’ll give a look to your link, thanks for that!

Viewing 4 posts - 1 through 4 (of 4 total)

The topic ‘Auto Prefill Fields based on EXIF’ is closed to new replies.