I am using acf_form() front end for my task and I have to rename files after saving the page with a prefix name, which is according to its field. So the $newfilename has now prefix ‘1_1’ and I am pulling my head for days to get prefix ‘2_1’ for group 2, too. I have been trying many ways but the file name will be the same prefix for group1 and group 2. (obviously in this code)
I was thinking to add prefix name to Wrapper Attributes of add file 1 (type repeater) and add file 2 (type repeater), but I cannot get it works. Does someone have any idea to solve the problem? Thanks
This my my ACF structure:
group1 (type group)
–add file 1 (type repeater)
—-add a new file ( type file) // Prefix name should be 1_1_file_name
group2 (type group)
–add file 2 (type repeater)
—-add a new file ( type file) // Prefix name should be 2_1_file_name
This is my code:
function rename_filename( $post_id ) {
$arg = array(
‘post_type’ => ‘attachment’,
‘post_id’ => $post_id,
);
$getPost = get_posts($arg);
foreach ($getPost as $key => $value) {
$attachmentID = $value->ID;
$file = get_attached_file($attachmentID);
$path = pathinfo($file);
//dirname = File Path
//basename = Filename.Extension
//extension = Extension
//filename = Filename
$newfilename = ‘1_1_’.$value->post_name;
$newfile = $path[‘dirname’].”/”.$newfilename.”.”.$path[‘extension’];
update_attached_file( $attachmentID, $newfile );
$attachmentAgrs = array(
‘ID’ => $attachmentID,
‘post_title’ => $newfilename,
‘guid’ => wp_upload_dir()[‘url’].’/’.$newfilename.’.’.$path[‘extension’],
);
wp_update_post( $attachmentAgrs );
}
}
add_action( ‘acf/save_post’, array($this, ‘rename_filename’), 12, 1 );
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Privacy Policy. If you continue to use this site, you consent to our use of cookies.