Best regards, I want to change the name of the automatic image with the title of the post.
For example, I have the image called 1231231252345.jpg and Title of the article is "Article for a test", I want to change the name of the image in "article-for-a-test.jpg"
Best regards, I want to change the name of the automatic image with the title of the post.
For example, I have the image called 1231231252345.jpg and Title of the article is "Article for a test", I want to change the name of the image in "article-for-a-test.jpg"
Share Improve this question edited Apr 9, 2019 at 13:17 norman.lol 3,2313 gold badges30 silver badges35 bronze badges asked Apr 9, 2019 at 12:36 Panait Andrei AlexandruPanait Andrei Alexandru 637 bronze badges2 Answers
Reset to default 6I think what you're looking for is the filter: wp_handle_upload_prefilter
.
From the Codex:
The single parameter,
$file
, represent a single element of the$_FILES
array. Thewp_handle_upload_prefilter
provides you with an opportunity to examine or alter the filename before the file is moved to its final location.
Example code from the Codex:
add_filter('wp_handle_upload_prefilter', 'custom_upload_filter' );
function custom_upload_filter( $file ){
$file['name'] = 'wordpress-is-awesome-' . $file['name'];
return $file;
}
How to use this hook
add_filter( 'wp_handle_upload_prefilter', 'custom_upload_filter' );
function custom_upload_filter( $file ) {
if ( ! isset( $_REQUEST['post_id'] ) ) {
return $file;
}
$id = intval( $_REQUEST['post_id'] );
$parent_post = get_post( $id );
$post_name = sanitize_title( $parent_post->post_title );
$file['name'] = $post_name . '-' . $file['name'];
return $file;
}
Found this article but unable to put it on place: https://wordpress/support/topic/how-to-auto-rename-based-on-current-post-title-a-media-defined-as-featured-image/