Skip to content

Instantly share code, notes, and snippets.

@Mallinanga
Created December 8, 2013 13:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Mallinanga/7857164 to your computer and use it in GitHub Desktop.
Save Mallinanga/7857164 to your computer and use it in GitHub Desktop.
WordPress Media Library rename files
<?php
if ( !defined( 'ABSPATH' ) ) {
exit;
}
add_action( 'init', 'rename_media_files_init' );
function rename_media_files_init() {
add_filter( 'attachment_fields_to_edit', 'rename_media_files_fields_to_edit', 11, 2 );
add_filter( 'attachment_fields_to_save', 'rename_media_files_attachment_fields_to_save', 11, 2 );
}
/**
* Add field on media edit screen
* @since 0.1
* @param array $form_fields Existing form fields
* @param object $post Current attachment post object
* @return array $form_fields New form fields
*/
function rename_media_files_fields_to_edit( $form_fields, $post ) {
/* Only show if not in Thickbox iframe */
if ( defined( 'IFRAME_REQUEST' ) && true === IFRAME_REQUEST ) {
return $form_fields;
}
rename_media_files_load_textdomain();
$orig_file = get_attached_file( $post->ID );
$orig_filename = basename( $orig_file );
$form_fields[ 'rename_media_files_input' ] = array( 'label' => __( 'New file name:', 'rename-media-files' ),
'value' => '',
'helps' => sprintf( __( 'Enter a new file name in the field above. (current filename is <strong>%1$s</strong>)', 'rename-media-files' ), $orig_filename ) );
return $form_fields;
}
/**
* Save form field value on media edit screen
* @since 0.1
* @param array $post Current attachment post data
* @param array $attachment Data submitted via form
* @return array $post Current attachment post data
*/
function rename_media_files_attachment_fields_to_save( $post, $attachment ) {
/* Only proceed if new filename is submitted */
if ( $attachment[ 'rename_media_files_input' ] ) {
/* Get original filename */
$orig_file = get_attached_file( $post[ 'ID' ] );
$orig_filename = basename( $orig_file );
/* Get original path of file */
$orig_dir_path = substr( $orig_file, 0, ( strrpos( $orig_file, "/" ) ) );
/* Get image sizes */
$image_sizes = array_merge( get_intermediate_image_sizes(), array( 'full' ) );
/* If image, get URLs to original sizes */
if ( wp_attachment_is_image( $post[ 'ID' ] ) ) {
$orig_image_urls = array();
foreach ( $image_sizes as $image_size ) {
$orig_image_data = wp_get_attachment_image_src( $post[ 'ID' ], $image_size );
$orig_image_urls[ $image_size ] = $orig_image_data[ 0 ];
}
/* Otherwise, get URL to original file */
} else {
$orig_attachment_url = wp_get_attachment_url( $post[ 'ID' ] );
}
/* Make new filename and path */
$new_filename = wp_unique_filename( $orig_dir_path, $attachment[ 'rename_media_files_input' ] );
$new_file = $orig_dir_path . "/" . $new_filename;
copy( $orig_file, $new_file );
update_attached_file( $post[ 'ID' ], $new_file );
$post_for_guid = get_post( $post[ 'ID' ] );
$guid = str_replace( $orig_filename, $new_filename, $post_for_guid->guid );
wp_update_post( array( 'ID' => $post[ 'ID' ],
'guid' => $guid ) );
/* Update attachment's metadata */
wp_update_attachment_metadata( $post[ 'ID' ], wp_generate_attachment_metadata( $post[ 'ID' ], $new_file ) );
global $wpdb;
if ( wp_attachment_is_image( $post[ 'ID' ] ) ) {
foreach ( $image_sizes as $image_size ) {
$orig_image_url = $orig_image_urls[ $image_size ];
$new_image_data = wp_get_attachment_image_src( $post[ 'ID' ], $image_size );
$new_image_url = $new_image_data[ 0 ];
$wpdb->query( $wpdb->prepare( "UPDATE $wpdb->posts SET post_content = REPLACE(post_content, '$orig_image_url', '$new_image_url');" ) );
}
} else {
$new_attachment_url = wp_get_attachment_url( $post[ 'ID' ] );
$wpdb->query( $wpdb->prepare( "UPDATE $wpdb->posts SET post_content = REPLACE(post_content, '$orig_attachment_url', '$new_attachment_url');" ) );
}
}
return $post;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment