Skip to content

Instantly share code, notes, and snippets.

@CapWebSolutions
Created May 22, 2018 14:00
Show Gist options
  • Select an option

  • Save CapWebSolutions/ff92cbbc2c214e7c332610abb1ba8a28 to your computer and use it in GitHub Desktop.

Select an option

Save CapWebSolutions/ff92cbbc2c214e7c332610abb1ba8a28 to your computer and use it in GitHub Desktop.
Change filename on upload for jpg and png files.
<?php
/**
* Plugin Name: Hash Upload Filename
* Plugin URI: https://stackoverflow.com/questions/3259696/rename-files-during-upload-within-wordpress-backend
* Description: Rename uploaded jpg and png files as the hash of their original. Extension remains same.
* Author: Web Savvy Marketing
* Author URI: http://www.web-savvy-marketing.com
* Version: 1.0
*/
/**
* Filter {@see wp_handle_upload_prefilter } and return an MD5 hash.
*
* @param string $filename
* @return string
*/
function wsm_wp_modify_uploaded_file_names($file) {
$info = pathinfo($file['name']);
$ext = strtolower( empty($info['extension']) ? '' : '.' . $info['extension'] );
$name = basename($file['name'], $ext);
if ( $ext == '.jpg' || $ext == '.png' ) $file['name'] = md5($name) . $ext; // md5 method
return $file;
}
add_filter( 'wp_handle_upload_prefilter', 'wsm_wp_modify_uploaded_file_names', 1, 1 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment