I will explain my problem: I have a post type called
Book with custom fields: Author, Genre, Year.
I name the fields (not the label) like:
It seemed good, until my client decided to change the name from Book to Issue! Now I should rename all the fields but so I lose the fields content!
So, maybe it’s better to name them just
year but so I could fall in some conflicts? Example, another group of custom fields with same name that point always to Book cpt?
So… what’s the best practice you use?
year will give you problems because these are “reserverd” words in WP. They’ll cause conflicts.
Besides worrying about any problems with WP as mentioned above, you really can’t have fields with the same name attached to the same post type or you’ll run into problems there as well. They tend to overwrite each other. Basically, every field you create, with the exception of sub fields in repeaters and flexible content, need to be unique.
I would not change the names of the fields if they already have data in them, actual field name does not matter…
unless you’re using the field name to do more than just display data, for example converting them to labels on the front end or something. I that’s the case then I’d go into the database using phpMyAdmin and to a query to replace all the old field name values with new ones, and you need to remember to rename the special ACF names as well.
$my_field_name there is also a
'_'.$my_field_name to worry about.
Thanks for answer @hube2
Then I will adapt my question: in your opinion is it better name the fields like
prefix_book_year (where prefix is a unique prefix, that could be my name or the name of my client’s company) or
book_year (where book identifies the post type)?
In the first case I can fall in conflicts if I will need to add another fields group to the same target.
I tend to use the second example, where the prefix matches the slug of the CPT I’m using them on.
book_authour etc. On options pages I use a prefix that indicates the type of option it’s for, for example
site_options_field_name. I have, in the past used a prefix that represented the client, but I stopped doing that when I started reusing field groups and needed to rename them all to match another client.
To be honest, I don’t think that there is a “standard WordPress practice” except that the top level fields should have a prefix and I try to make sure that my field names will not conflict with any of the more popular plugins, and I like using things that will be reusable.
i tend to use a neutral prefix for all fields created by me & followed by the slug of cpt as second prefix.
would be for example:
with that i can be 99% sure that i dont conflict with other fields.
i am also able to reuse the “fields/code” for an other customer, without the need to rewrite anything.
You must be logged in to reply to this topic.
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!
#DropzoneJS is now integrated into @ws_form allowing you to create no-code drag and drop file upload fields on your #WordPress forms. Fully integrated with @wp_acf. Fully responsive. Selfie feature! Learn more here: https://t.co/JUQmH2z315 pic.twitter.com/JhfO0LVQCm— WS Form (@ws_form) January 8, 2021
© 2021 Advanced Custom Fields. Subscribe