Is there a function that allows me to change the filename of an attachment, based on the Attachment ID I have?
Thanks!
Dennis
Is there a function that allows me to change the filename of an attachment, based on the Attachment ID I have?
Thanks!
Dennis
You must be logged in to post a comment.
This will allow you to rename an attachment as soon as its uploaded:
Use Cases
The function works for
No-Use Cases
It aborts for autosave jobs, performed by wordpress automagically or if the targeted file types or mime types are not met.
Goodies
You can set the file name, file types & mime types that you want to change inside the
function before the
foreach
loop. The file gets the post ID and then the attachment ID appended, so you can safely upload and change multiple files at a time. This also cares about ordering the files by (first) post ID and (second) attachment ID.The function should be added to your functions.php file or (better) as a separate small plugin. Just add a plugin comment on top, upload it to the plugins folder and activate.
I’d use PHP’s
rename
and the path to the file given byget_attached_file
.NOTE that this has been not tested and you should extreme precaution when working whith files. It probably needs changing for it to work but may be a good starting point. Hope this helps.
Let me know if it helps and I’ll change the code to the actual working code.
Reference
http://codex.wordpress.org/Function_Reference/update_attached_file
http://wordpress.org/tags/add_attachment