Skip to content
Read For Learn
Read For Learn
  • Database
    • Oracle
    • SQL
  • C
  • C++
  • Java
  • Java Script
  • jQuery
  • PHP
Read For Learn
  • Database
    • Oracle
    • SQL
  • C
  • C++
  • Java
  • Java Script
  • jQuery
  • PHP

How to save EXIF metadata in WordPress database?

I’ve got the help from a guru on other forum. The following code add fields ‘City’ and ‘Location name’ to attachment edit page. Their values gets from IPTC metadata (Tag: 90, Key: Iptc.Application2.City / Tag: 27, Key: Iptc.Application2.LocationName) and automatically save it in db on image upload (Custom fields: ‘city‘ and ‘locationname‘). The values can be modified manually on the same attachment edit page.

 function wp_read_image_metadata_exif( $file ) {
    if ( ! file_exists( $file ) ) {
        return false;
    }

    list( , , $image_type ) = wp_getimagesize( $file );

    if ( is_callable( 'iptcparse' ) ) {
        wp_getimagesize( $file, $info );

        if ( ! empty( $info['APP13'] ) ) {

            if ( defined( 'WP_DEBUG' ) && WP_DEBUG
                && ! defined( 'WP_RUN_CORE_TESTS' )
            ) {
                $iptc = iptcparse( $info['APP13'] );
            } else {
                $iptc = @iptcparse( $info['APP13'] );
            }
            
            if ( ! empty( $iptc['2#090'][0] ) ) { // City.
                $meta['city'] = trim( $iptc['2#090'][0] );
            }
            if ( ! empty( $iptc['2#027'][0] ) ) { // Location Name.
                $meta['locationname'] = trim( $iptc['2#027'][0] );
            }
        }
    }

    return apply_filters( 'wp_read_image_metadata_exif', $meta, $file, $iptc );

 }

 function display_exif_fields ( $form_fields, $post ){
                
    $city = get_post_meta( $post->ID, 'city', true );   
    
    $locationname = get_post_meta( $post->ID, 'locationname', true );
       
    $form_fields['city'] = array(
      'label' => 'City',
      'input' => 'text',
      'value' => $city,
      'helps' => '',
    );
  
    $form_fields['locationname'] = array(
      'label' => 'Location name',
      'input' => 'text',
      'value' => $locationname,
      'helps' => '',
    );

    return $form_fields;
  }

add_filter( 'attachment_fields_to_edit', 'display_exif_fields', 10, 2 );


function save_exif_fields( $post, $attachment ) {

    $array = [ 'city', 'locationname' ];
    foreach ( $array as $one ) {
      if ( ! empty( $attachment[ $one ] ) ) {
        update_post_meta( $post[ 'ID' ], $one, $attachment[ $one ] );
      } else {
        delete_post_meta( $post[ 'ID' ], $one );
      }
    }

    return $post;   
 }

 add_filter( 'attachment_fields_to_save', 'save_exif_fields', 10, 2 );


 add_action('added_post_meta', 'save_exif_fields_on_upload', 10, 4);

 function save_exif_fields_on_upload($meta_id, $post_id, $meta_key, $meta_value) {
        
    $type = get_post_mime_type( $post_id );

    $attachment_path = get_attached_file( $post_id );

    $metadata = wp_read_image_metadata_exif( $attachment_path );
        
    if($meta_key === '_wp_attachment_metadata') {   

        update_post_meta($post_id, 'city', $metadata['city']);
        update_post_meta($post_id, 'locationname', $metadata['locationname']);
           
        $attachment_meta = get_post_meta($post_id);
    }
 }

Related Posts:

  1. Why WordPress choose data serialization over json_encode?
  2. Dealing with Many Meta Values, 30+
  3. What is the most secure way to store post meta data in WP?
  4. How to delete outdated, wrongly sized images in _wp_attachment_metadata?
  5. Meta query interfering with orderby relevance
  6. Migrating non-WordPress CMS to WordPress, lots of data to move — possible solutions?
  7. How to solve slow WordPress site caused by attachment_metadata
  8. Delete all post meta except featured image
  9. Is it possible to store visitors IPs in wp_postmeta table?
  10. Multiple postmeta with same name for one post in wp_postmeta table
  11. Delete all post meta except featured image Using SQL
  12. One post carries 30 postmeta values, is this too much?
  13. delete post meta from db, even if does not exist?
  14. WordPress and MySQL: how to transfer Meta_key and Meta_Value from one post_id to another
  15. Storing data for a wordpress plugin
  16. Where does the actual data stored by add_post_meta
  17. Structure of postmeta meta_value for woocommerce product download
  18. Routine to convert custom post meta from old to new value
  19. Saving zero as meta value
  20. Uknown meta entries in wp_postmeta
  21. Does post_meta data need to be unserialzed?
  22. Link image with data from the wpdb
  23. Custom Post Type not showing anything added by code anymore?
  24. Compare User meta_value with Product meta_value
  25. _wp_attachment_metadata is not being added to database when PDF files uploaded
  26. “BS_” rows in postmeta table
  27. Migrating meta_key and meta_value from old theme to new one
  28. Database structure for thousands of posts
  29. How much post meta data is too much?
  30. How do i get value from wp_postmeta?
  31. Recommended Post Structure for DB Storage
  32. SQL query : Select a thumbnail for a media
  33. What is an ORM, how does it work, and how should I use one? [closed]
  34. What is this JavaScript “require”?
  35. Database vs File system storage
  36. WPDB Insert or if exists Update
  37. WP Multisite: load content from site X on site Y
  38. How does $wpdb handle COUNT(*)
  39. Shared Members between two different wordpress installations with different databases
  40. importing data from non-wordpress mysql db
  41. Efficiency on displaying random authors based on large database
  42. How to write Hebrew characters?
  43. Error establishing a database connection – with Debug Data
  44. Finding the login page from database?
  45. How to execute mulitple statement SQL queries using $wpdb->query?
  46. Store and Encrypt Contact Form 7 Submissions in Database? [closed]
  47. When and why should I use $wpdb different return types?
  48. Database with mixed collation (utf8mb4 & utf8_general_ci)
  49. WordPress database growing dramatically in size despite adding no new content
  50. Clean database from unused tables
  51. How wordpress accesses its own database
  52. Migrating database / content of non-CMS site to WordPress
  53. Adding new row to wp_post table
  54. Extracting the post_id via the wp_insert_post action (external db query)
  55. How to interact with another wordpress install on same database?
  56. Saving into post_excerpt or post_content
  57. Maintain user profile data in a non-WordPress database
  58. How to fix the disappearance of the “\” character when updating a post in the database
  59. How to scan barcode and store data into a database [closed]
  60. How can I push local database changes to production, without impacting recent posts/pages that were published on production server?
  61. Setup private content for specific users with daily updates [closed]
  62. Query multiple tables at once?
  63. Transferring/Uploading Data from DB to WordPress
  64. how to encyrpt DB_PASSWORD in wp-config
  65. Choosing http or https during manual installation of WordPress and database
  66. Delete all posts that do NOT have a specific tag
  67. Changing order of db results when encoding to JSON
  68. How to transfer from localHost to live but use the already existing database on the server?
  69. Why does wp_links still exist in the db schema?
  70. can’t upgrade wordpress or install plugins, it seems to “think” it’s still on a local installation
  71. How not to display ALL items of a database
  72. How to get the records from start with A to G in php and mysql
  73. How to escape percentage sign(%) in sql query with $wpdb->prepare?
  74. WordPress has been updated! Before we send you on your way, we have to update your database to the newest version
  75. Malware in old website – how to migrate?
  76. A WP dev site that displays content from a live site’s database but cannot write to wp_posts?
  77. update_post_meta performance in a loop woocommerce
  78. error establishing database connection (WAMP + filezilla)
  79. Is there a way for two deferent themes to consume two different DB on a same WP instance?
  80. How do I update a WordPress wp_postmeta meta_value that contains many options
  81. How to display $wpdb->insert_id; value on redirected page
  82. Should I Use only wpdb Class to Write Custom Queries?
  83. Auto populate form fields based on serial input or pull listings from other websites?
  84. How to extract some part of WordPress full source code
  85. Error establishing a database connection for some files
  86. Hash user emails in database?
  87. Looking for suggestions on creating simple database (Help!!)
  88. Weird characters in title if it contain “ ”
  89. DB access blocked when initializing WP externally
  90. Saving data to database and exporting to excel
  91. Site does not work right, pages not showing up, even for root admin
  92. One information repeated on multiple locations on one site [closed]
  93. WordPress: Interact with Database Query Data and Login
  94. Showing Error(TAble already exist)
  95. Getting an error when trying to migrate to DV server from Grid with Media Temple [closed]
  96. Cant Connect to Database
  97. WordPress database connection failed, while mysql client is working [closed]
  98. update_post_meta returns ID, but nothing in the database
  99. Combining two select posts from mysql – to get least viewed posts
  100. DB table creation on reactivation – dbDelta errors on CONSTRAINTS
Categories Database Tags database, post-meta
How to access wordpress from domainB which is installed at domainA
Template for Custom Post for the EDITOR

Recommended Hostings

Cloudways: Realize Your Website's Potential With Flexible & Affordable Hosting. 24/7/365 Support, Managed Security, Automated Backups, and 24/7 Real-time Monitoring.

FastComet: Fast SSD Hosting, Free Migration, Hack-Free Security, 24/7 Super Fast Support, 45 Day Money Back Guarantee.

Recent Added Topics

  • Bug in translation system: load_theme_textdomain() returns true, files are available and accessible but the language defaults to english
  • Custom Elementor controls not appearing in the widget Advanced tab using injection hooks
  • Get the name of the template/*html file used
  • Trying to Add Paging to Single Post Page
  • Sharing media files between live and staging servers
  • How to display the description of a custom post type in the dashboard?
  • Critical error on image display
  • Copying WP data and files into new install?
  • How to determine the DirectAdmin WordPress backup date?
  • How to get list of ALL tables in the database?
© 2026 Read For Learn
  • Database
    • Oracle
    • SQL
  • algorithm
  • asp.net
  • assembly
  • binary
  • c#
  • Git
  • hex
  • HTML
  • iOS
  • language angnostic
  • math
  • matlab
  • Tips & Trick
  • Tools
  • windows
  • C
  • C++
  • Java
  • javascript
  • Python
  • R
  • Java Script
  • jQuery
  • PHP
  • WordPress