What the similar posts (wich your found) are suggesting, is adding a filter in your functions.php
wich will handle the filenames for you. All uploaded files will be filtered by this function before the upload will be handled by wp_handle_upload
.
Add the following lines of code in your functions.php
:
function wpse_82741_rename_uploaded_file( $image ) {
global $post;
// get the post ID
$post_id = absint( $_REQUEST['post_id'] );
// get the image extension
switch( $image['type'] ) {
case 'image/jpeg' :
$extension = 'jpg';
break;
case 'image/png' :
$extension = 'png';
break;
case 'image/gif' :
$extension = 'gif';
break;
}
// set the new image name
$image['name'] = "$post_id.$extension";
}
add_filter( 'wp_handle_upload_prefilter', 'wpse_82741_rename_uploaded_file', 20 );
And all names will be like {post_id}.{extension}
.
For the handling of your upload path, there is a question about this.
Upload path handling goes through
wp_upload_dir()
that appliesupload_dir
filter to returned information. You should try filtering it for the duration of your code running and adjusting paths to wanted location.
You could try this by using the code explained on this webpage by adding the following code in your functions.php
and edit the configuration:
add_filter('upload_dir', 'wpse_82741_upload_dir');
$upload = wp_upload_dir();
remove_filter('upload_dir', 'wpse_82741_upload_dir');
funcion my_upload_dir($upload) {
$upload['subdir'] = '/sub-dir-to-use' . $upload['subdir'];
$upload['path'] = $upload['basedir'] . $upload['subdir'];
$upload['url'] = $upload['baseurl'] . $upload['subdir'];
return $upload;
}
It is quite a job to accomplish this, but i’m sure it is possible.