By Janina


2019-07-08 21:12:18 8 Comments

Currently Wordpress gets an attachment name from the exif title value for images:

<?php
// Use image exif/iptc data for title and caption defaults if possible.
} elseif ( 0 === strpos( $type, 'image/' ) && $image_meta = wp_read_image_metadata( $file ) ) {
    if ( trim( $image_meta['title'] ) && ! is_numeric( sanitize_title( $image_meta['title'] ) ) ) {
        $title = $image_meta['title'];
    }

Is there any way to save the original filename to the database? I've tried bunch of filters and hooks without any result.

https://developer.wordpress.org/reference/functions/media_handle_upload/

Possible solution could be to use sanitize_file_name filter to get $filename_raw and insert it to custom table with the sanitized $filename.

To get the original filename later we just need to query the custom table while comparing sanitized filenames to attachment filenames.

Am I right or is there any other way?

1 comments

@Howdy_McGee 2019-07-08 22:21:37

How about saving the original filename inside the current $_REQUEST and processing it whenever WordPress generates the attachment metadata:

/**
 * Store original attachment filename in the current $_REQUEST superglobal
 * 
 * @param NULL
 * @param Array $file - Uploaded File Information
 * 
 * @return NULL
 */
function wpse342438_store_attachment_filename( $null, $file ) {

    $_REQUEST['original_filename'] = basename( $file['name'] );

    return $null;

}
add_filter( 'pre_move_uploaded_file', 'wpse342438_store_attachment_filename', 10, 2 );

/**
 * Save original filename if it exists in our $_REQUEST
 * 
 * @param Array $metadata           - Generated Attachment Metadata
 * @param Integer $attachment_id    - WordPress Attachment Post ID
 * 
 * @return Array $metadata
 */
function wpse342438_save_original_filename( $metadata, $attachment_id ) {

    if( ! empty( $_REQUEST['original_filename'] ) ) {
        update_post_meta( $attachment_id, '_original_filename', sanitize_text_field( $_REQUEST['original_filename'] ) );
    }

    return $metadata;

}
add_filter( 'wp_generate_attachment_metadata', 'wpse342438_save_original_filename', 10, 2 );

The pre_move_uploaded_file hook should fire before your uploaded file moves from the tmp directory to the actual WordPress uploads folder structure. The wp_generate_attachment_metadata hook should run every time an attached is added to the database so it can created related postmeta. If the original_filename key exists in the request by the time we get here, we can set it.

Related Questions

Sponsored Content

1 Answered Questions

2 Answered Questions

[SOLVED] how to get original image using wp_get_attachment_image_src

1 Answered Questions

[SOLVED] How to get the filename from file system and create a download link?

3 Answered Questions

[SOLVED] How to allow empty title for attachments?

  • 2012-08-23 13:31:16
  • ptriek
  • 312 View
  • 1 Score
  • 3 Answer
  • Tags:   attachments

1 Answered Questions

1 Answered Questions

[SOLVED] How do i save and retrieve custom attachment meta?

2 Answered Questions

[SOLVED] How to cleanly delete attachment but retain original file?

  • 2011-02-17 14:28:41
  • Rarst
  • 2205 View
  • 6 Score
  • 2 Answer
  • Tags:   attachments

1 Answered Questions

[SOLVED] Strip the folder path away from wp_get_attachment_url to show filename only

  • 2011-01-14 19:18:40
  • John Chandler
  • 1527 View
  • 0 Score
  • 1 Answer
  • Tags:   attachments

Sponsored Content