Hi Luo, thanks for the suggestion. This is the code I've set up:
$cv = get_post_meta( $post_id, 'wpcf-cv', true );
$cv2 = str_replace("/uploads/", "/uploads/candidato/", $cv);
$cv = wp_make_link_relative($cv);
$cv_home = str_replace("/wp-content/uploads/", "/home/rubikcom/public_html/wp-content/uploads/", $cv);
$cv2_home = str_replace("/wp-content/uploads/", "/home/rubikcom/public_html/wp-content/uploads/candidato/", $cv);
rename($cv_home, $cv2_home);
update_post_meta( $post_id, 'wpcf-cv', $cv2 );
It makes the job but there are some issues:
- The user will upload PDF or DOCX files, I've tested with some PDF and when I download the uploaded file from the frontend, it is a corrupted file that PDF viewer cannot open.
- WordPress is still creating the post on Media with the wrong file URL and it is also creating thumbnails in /uploads/ directory.
I've tried with this code:
$files = get_attached_media( '', $post_id );
foreach($files as $file) {
$file_id = $file->ID;
}
$cv = wp_get_attachment_url( $file_id );
$cv2 = str_replace("/uploads/", "/uploads/candidato/", $cv);
$cv = wp_make_link_relative($cv);
$cv_home = str_replace("/wp-content/uploads/", "/home/rubikcom/public_html/wp-content/uploads/", $cv);
$cv2_home = str_replace("/wp-content/uploads/", "/home/rubikcom/public_html/wp-content/uploads/candidato/", $cv);
rename($cv_home, $cv2_home);
update_post_meta( $post_id, 'wpcf-cv', $cv2 );
update_attached_file( $file_id, $cv2 );
It works and it also set the correct URL in the Media post, but it still corrupts the uploaded files and it still creates thumbnails in /uploads/ directory.
Any idea how to prevent file corruption? Is there a way to prevent thumbnail creation or move them to /uploads/candidato/?
Thnaks