Skip to content

Instantly share code, notes, and snippets.

@numanturle
Created August 30, 2019 07:50
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 numanturle/167b60a9f7878f3a7e9cf3c008492803 to your computer and use it in GitHub Desktop.
Save numanturle/167b60a9f7878f3a7e9cf3c008492803 to your computer and use it in GitHub Desktop.
wordpress thumbnail
<?php
add_action('add_attachment', 'rename_attachment');
function rename_attachment($post_ID){
$file = get_attached_file($post_ID);
$get_file_title = get_post($post_ID);
if($get_file_title->post_parent){
$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