Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
wordpress thumbnail
add_action('add_attachment', 'rename_attachment');
function rename_attachment($post_ID){
$file = get_attached_file($post_ID);
$get_file_title = get_post($post_ID);
$post_santize_title = sanitize_title(get_the_title($get_file_title->post_parent));
$path = pathinfo($file);
$newfilename = $post_santize_title."-".$post_ID;
$newfile = $path['dirname']."/".$newfilename.".".$path['extension'];
rename($file, $newfile);
update_attached_file( $post_ID, $newfile );
$my_post = array(
'ID' => $post_ID,
'post_title' => $newfilename,
wp_update_post( $my_post );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.