WordPress.org

Ready to get started?Download WordPress

Forums

Extracting EXIF and inserting into taxonomy (4 posts)

  1. Austin Passy
    Member
    Posted 4 years ago #

    I created a custom taxonmy called location and registered it to "attachment:image" only.

    I also added a filter to read Geo Location EXIF data from an image is present.

    What I'd like to know now is how to add the geo meta data to the taxonmy "location" in that attachment ID.

    Thoughts? I can supply function code if needed.

  2. Austin Passy
    Member
    Posted 4 years ago #

    After more searching I imagine this function would suite best: <?php wp_update_attachment_metadata( $post_id, $data ) ?>

    Just need to figure out how to write it up.

  3. Austin Passy
    Member
    Posted 4 years ago #

    Maybe I can write each attachment location with ID in the $post->ID custom field as an array and extract it later..

  4. Austin Passy
    Member
    Posted 4 years ago #

    Well I've been at it for 24 hours now, and I'm just lost in code. I think the best way to do this is by reading the meta data and using the wp_set_object_terms to write the meta into the taxonomy.

    Let me show you one of the many functions I've written up:

    function addTerm($id, $tax = 'location', $term = 'location') {
    	global $post;
    
    	$args = array(
    			'post_type' => 'attachment',
    			'numberposts' => -1,
    			'post_status' => null,
    			'post_parent' => $post->ID
    		);
    
    		$attachments = get_posts($args);
    		if ($attachments) {
    			foreach ( $attachments as $attachment ) {
    				$imgid = $attachment->ID;
    				$imgmeta = wp_get_attachment_metadata( $imgid );
    
    				if ( !empty( $imgmeta ) ) {
    					$latitude = $imgmeta['image_meta']['latitude'];
    					$longitude = $imgmeta['image_meta']['longitude'];
    					$lat_ref = $imgmeta['image_meta']['latitude_ref'];
    					$lng_ref = $imgmeta['image_meta']['longitude_ref'];
    					$lat = geo_single_fracs2dec($latitude);
    					$lng = geo_single_fracs2dec($longitude);
    					if ($lat_ref == 'S') { $neg_lat = '-'; } else { $neg_lat = ''; }
    					if ($lng_ref == 'W') { $neg_lng = '-'; } else { $neg_lng = ''; }
    
    				if ($latitude != 0 && $longitude != 0)
    					$term == $neg_lat . number_format($lat,6) . ', ' . $neg_lng . number_format($lng, 6);
    				}
    			}
    		}
    
        $term_id = term_exists($term);
        $term_id = intval($term_id);
        if (!$term_id) {
            $term_id = wp_insert_term($term, $tax);
            $term_id = $term_id;
            $term_id = intval($term_id);
        }
    
        // get the list of terms already on this object:
        $terms = wp_get_object_terms($id, $tax)
        //$terms[] = $term_id;
        $terms[] = $term;
    
        $result = wp_set_object_terms($id, $terms, $tax, FALSE);
    
        return $result;
    }
    
    //add_action('edit_attachment', 'addTerm');
    add_action('publish_post', 'addTerm');

    Maybe you'll see what I can't?

Topic Closed

This topic has been closed to new replies.

About this Topic