barakadam 님의 답변은 거의 정확합니다. 답변을 아래에 남겨 두었던 의견을 기반으로 작성되었습니다. 코드의
function new_filename($filename, $filename_raw) {
global $post;
$info = pathinfo($filename);
$ext = empty($info['extension']) ? '' : '.' . $info['extension'];
$new = $post->post_title . $ext;
// the if is to make sure the script goes into an indefinate loop
if($new != $filename_raw) {
$new = sanitize_file_name($new);
}
return $new;
}
add_filter('sanitize_file_name', 'new_filename', 10, 2);
설명 :
는 "파리/런던 내 휴가"라는 게시물에 picture one.jpg
라는 원래의 파일 이름으로 파일을 업로드 가정 할 수 있습니다.
파일을 업로드 할 때 WordPress는 sanitize_file_name()
기능을 사용하여 원래 파일 이름에서 특수 문자를 제거합니다.
오른쪽 하단에 필터가 있습니다.
// line 854 of wp-includes/formatting.php
return apply_filters('sanitize_file_name', $filename, $filename_raw);
이 시점에서 $ filename은 picture-one.jpg
이됩니다. add_filter()
을 사용했기 때문에 new_filename() 함수는 $ filename을 picture-one.jpg
으로, $ filename_raw를 picture one.jpg
으로 호출합니다.
그러면 new_filename() 함수는 파일 이름을 원래 확장자가 추가 된 게시물 제목으로 바꿉니다. 여기에서 끝내면 새 파일 이름 $new
은 결국 우리 모두가 알고있는 유효하지 않은 파일 이름 인 My Holiday in Paris/London.jpg
이됩니다.
sanitize_file_name 함수를 다시 호출 할 때입니다. 거기에 조건문을 기록하십시오. 이 시점에서 $new != $filename_raw
부터 파일 이름을 다시 살균합니다.
sanitize_file_name()
가 호출되고 함수의 끝에서,
$filename
는
My-Holiday-in-Paris-London.jpg
$filename_raw
동안 여전히
My Holiday in Paris/London.jpg
을 것이다 될 것이다.
apply_filters()
때문에 우리의
new_filename()
함수가 다시 실행됩니다. 그러나 이번에는
$new == $filename_raw
이기 때문에 끝납니다.
그리고 마지막으로 My-Holiday-in-Paris-London.jpg
가 반환됩니다.
+1 멋진 글쓰기! 같은 파일이 두 번 업로드되면 어떻게됩니까? – brasofilo
대단하군요! 의도 한대로 작동하는 것 같습니다. – jonbennett
@brasofilo이 시나리오를 볼 수 있습니까? [link] (http://stackoverflow.com/questions/13809555/rename-files-during-upload-within-wordpress-and-gravity-form) – jonbennett