Home Forums General Issues Rename and relocate uploaded Images


Rename and relocate uploaded Images

  • @hube2 esp.,

    Concerning these previous threads…

    … I had wanted to take an image uploaded to an ACF Image field, then a) move it to a specific sub-folder and b) rename it.

    I took my learnings from these threads and other online info and worked up some code. I kept this code in a dedicated plugin.

    But that code was designed to work only on an Edit-User page – that is, when a user uploads an Image to whilst on user-edit.php or profile.php, a) relocate it to a sub-folder of uploads, users/avatars, then b) rename it as the user slug dot extension.

    I had also made a separate version of this plugin to run on the Edit Term (term.php) page for a custom taxonomy (Company) – ie. rename the image to termslug.ext and relocate to /wp-content/uploads/companies/logos. This worked, too – until this week, though that’s not the nub of my post…

    See the plugins I just posted on GitHub FYI (cxt-wp-user-avatar, cxt-wp-tax-company-logo – but, really speaking, the first one, which actually functions currently).

    The trouble is, I can see reason for consolidation…

    Right now, each plugin carries hard-coded settings variables for the relevant ACF field, new-name composition and destination to which to move the renamed file.

    That’s not flexible, either for the multiple use cases I may have for this type of thing or for other people who may want to use such code.

    Both variants of this code do basically the same thing – take an uploaded ACF Image file, rename it according to the slug/name of the current WP “object” (user, taxonomy term) and move it under a corresponding sub-folder of uploads.

    I even have a third instance of this task I want to budget for – performing the same on a second Company term Image field, “favicon”.

    Do you have any thoughts on how I could achieve consolidation – that is, using one flexible plugin/method that can align the basic function here for all scenarios?

    The core purpose here is basically Media Library / image folder management efficiency/automation. It’s sort of something I maybe feel should be baked in to WP but isn’t, so I see room for an extension.


    * Applicability to all ACF Image fields?

    * Achievability of doing so. ie given the number of pages on which an Image field could be placed, is it possible for code to find and use a “slug” or equivalent in every case?

    * How to identify the field key and field name automatically? Currently these are hard-coded, needed to be known in advance. Can this requirement be removed?

    * How to identify corresponding sub-folders, if removing the requirement for pre-known location settings? eg. Should they follow a standard sub-folder naming convention, eg. /wp-content/uploads/{object}/{fieldname} (/wp-content/uploads/{user}/{avatar}/{username.ext} but also /wp-content/uploads/{taxonomy}/{company}/{logo}/{term.ext}?

    * Would one really want this to apply to all ACF Image fields, or only some?

    * Extent to which this is an ACF thing vs WP.

    PS. I am a git and GitHub newbie. I’m trying to learn some proper development and version control practices, and to be able to store code like this in repositories for pulling down to multiple sites. Also trying to put the code out in public a little. Please feel free to let me know if I should be doing anything differently here, including on attribution.


  • I would start here

    I don’t recall all of the specifics of renaming and altering the upload path. But with field settings added you can then have a single filter or action for all image fields. Then you can check the fields settings and rename and alter the path according to those field settings.

  • Hmm…
    So, you can add a custom setting to a field, to all Images…
    I guess you’re saying I would hitch a new field to every Image field in the admin.

    I’m not sure if that would achieve the aim (would it?). Maybe I don’t want to work on *all* Images, ie. if the user has more than one Image field/purpose on-site…

    If the aim is to relocate images uploaded to the Image field in a group that sits on the term-edit page for all Company taxonomy terms… don’t I need to set a folder path value at the Company (taxonomy) level (ie. wp-content/uploads/{specific-folder}/{specific-subfolder}`)?

    I don’t currently have any “taxonomy” (Company) page on which I think a group/field could be added. Unless ACF can put anything on the listing page edit-tags.php?taxonomy=company … Yes, I note ACF Extended enables support for <a href=””>Taxonomy List</a>.

    Hmm, should I be putting a Text field on Taxonomy List for “Logo Image Directory” (eg. “/company/logos“)?

    If I then filter the actual uploads, I’d need some way to check against the above value to get the directory… ?

    Seems a bit convoluted and not necessarily like I could make it a neat plugin package… (?).

    Rather, maybe I shouldn’t bother with <em>specifying</em> an image directory and just <em>infer</em> it…

    ie. 1 /wp-content/uploads/{taxonomy_slug}/{acf_field_name}/{term_slug.ext} ?

    eg. 1 /wp-content/uploads/company/logo/microsoft.png ?

    Yes, though then I think I need to overcome two things…

    <strong>A. Application to other objects beside taxonomy…</strong>

    ie. 2. /wp-content/uploads/user/{acf_field_name}/{username.ext}

    eg. 2 /wp-content/uploads/user/avatar/billgates.png ?

    Achieved through some conditionals/check against object type? Not certain how, maybe the same way I’m already getting the URL parameters.

    <strong>B. Any way to avoid hard-coding/pre-specifying the field name and field key?</strong>

    Can “field_6140a6da30a17” and “avatar“/”logo" be auto-obtained, so that this could just work on anything, I wonder?

    Thinking in public.

  • To answer one of your questions, creating a text field for the upload path was one of the setting I thought about when I replied. Honestly, I don’t know the specifics.

    In the upload pr-filter, I think I posted this somewhere in the past acf/upload_prefilter the field ID that the image is being uploaded for is in either the $_GET or $_POST array, I don’t remember which and I don’t remember the array index without testing. In addition to this I believe the object type and ID is also amongst the data available in the request. This value can be used to get the field object that includes any custom settings.

    I don’ know if you want to do this with field settings or you want to figure out how to do it automatically for every image field. Field settings would give you some configuration ability.

    Of course I don’t have the details on doing any of this. It would be a rather large project to investigate the options and I don’t currently have the….. resources to do so. Just throwing ideas out there base on some of the work I’ve done with altering upload paths.

  • Thanks for the pointers.

    Maybe last thing… another way to maybe do it…

    Is there any way to create/store some kind of i) hidden text field and value or ii) field meta information against my Image field on the admin side… ?

    If I could add a “file path” value to the specific Image field itself (to the field “logo”), maybe I could use that to write in and store the value of the uploads child folder for that field (“company/logo”).

    I’m talking about alongside a field’s other standard ACF settings like sizes and “Required?”.

    If so, maybe this is a simpler way to do it than creating a new field group just to put it on edit-tags.php?taxonomy=company

    I see ACF Extended offers a Hidden field that include an admin-side box “Value
    – Default value in the hidden input”…

    But a) this is still separate from my Image field itself and b) it seems designed for accommodating hidden HTML input form fields (perhaps it just stores a piece of arbitrary “Value” that you can render as you like; I’m not sure yet).

  • When adding field settings like I first suggested does not add an input for the user. It only affects the admin when you are editing the field settings. As an admin when you create the field you would decide if the image is uploaded to a different location or renamed. Yes it means having this setting on all image fields, but if done correctly it could just be ignored if the field setting is empty or not set.

Viewing 6 posts - 1 through 6 (of 6 total)

You must be logged in to reply to this topic.