Home Forums General Issues Best practice for naming the fields


Best practice for naming the fields

  • 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: book_author, book_genre, book_year.

    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 author, genre, 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?

  • just using author and 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.

    for every $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_author, prefix_genre, prefix_book_year (where prefix is a unique prefix, that could be my name or the name of my client’s company) or book_author, book_genre, 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: myacf_book_author, ...

    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.

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

You must be logged in to reply to this topic.