Created
August 30, 2019 07:50
-
-
Save numanturle/167b60a9f7878f3a7e9cf3c008492803 to your computer and use it in GitHub Desktop.
wordpress thumbnail
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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