A
A
Alexander Sobolev2018-06-14 15:54:41
WordPress
Alexander Sobolev, 2018-06-14 15:54:41

WP custom field options. How to write Custom field label?

I write ajax generation of arbitrary fields directly by the user "on the fly".
When creating, using the add_post_meta( $post_id, $meta_key, $meta_value, $unique ) function, we drive in the post id, key, value and uniqueness)
Question: where, how, and where is the label (Name) driven in?
If it is driven in as a key, then it turns out that if the user wants to edit only the name of the field, the script must change the key ($meta_key) - and this is, in fact, creating a new and deleting the current (changing) custom field?
Or is the label of an arbitrary field somehow and somewhere prescribed?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
I
Igor Vorotnev, 2018-06-14
@HeadOnFire

Label in this case is just a string that you display in the UI? That is, each created field will have 2 "parameters" - label (what is displayed next to the field) and value (what is entered in the field)? I see another bottleneck here - when you get to fields of other types (number, radio, checkbox, etc.), then you will have additional "parameters" - the field type and other properties. And here everything will become much more interesting :)
There is such an option - to store 2 records for one field in postmeta at once:
fieldname => field value (value)
fieldname_label => label value (label)
Fields can be named differently. For example:
fieldname => value
_fieldname = label
In this case, the field that starts with an underscore is considered "hidden" and "system". You can add a suffix. Well, then you need a bunch of "subsystems" to make it convenient to work with.
And there is another option - to store all (any) necessary information in the field value as a serialized array. The obvious disadvantage is that it will be difficult to search for such a field (however, external indexing using the same Elastic Search will solve the problem). For the rest - shove whatever you want there - the value of the field, its label, placeholder, character limit, and so on. Yes, even the entire html.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question