Dynamic Custom Fields Via JSON
- This topic has 3 replies, 2 voices, and was last updated 3 years, 9 months ago by
Long Nguyen.
-
AuthorPosts
-
July 8, 2021 at 11:08 PM #29384
TopDog
ParticipantI want to integrate a 3rd party software my WP install. Here's my challenge.
The software returns an array of JSON key pairs with the following characteristics:
1. Random quantity of JSON key pair arrays based on user interaction on the 3rd party platform
2. Multiple field types but limited to only four (4) types: text, number, url, color
3. Random custom field valuesThe challenge is the random number of JSON key pair arrays which means I don't know in advance how many custom fields per post I'll need. Although it will likely be less than 10 most times and almost never more than 25.
My question is what is the best approach for a solution to this challenge? I can think of two options:
- Programmatically create Metabox.io custom fields based on the returned JSON key pairs.
My issues with this option are: Is this even possible without massive custom coding? Will this create an unnecessary and bloated custom field structure that eventually is unsustainable? - Pre-create 25 - 50 custom fields in Metabox and use the JSON to simply update the fields.
My question with this option is, "Do/Can 30 - 40 empty/null custom fields significantly slow down WP?"
July 9, 2021 at 11:21 AM #29391Long Nguyen
ModeratorHi,
Thank you for reaching out.
How the key pairs relate to the meta boxes, 4 field types, and random value? Can you please give an example?
July 10, 2021 at 7:00 AM #29403TopDog
ParticipantThe 4 field types are: text/string, number, color code (which I guess could be text), image. The code has a true/false option but that isn't necessary to use for my purpose.
It's an image API so all the fields are attributes of one image. It's a multilevel array of attributes.
Here's a short example of the JSON. Any one image would have a random number of attributes/key pairs.
{ "template_name": "Facebook-Post-1200x900", "api_key": "XXXXXXXXXXXXXX", "modifications": [ { "name": "background-image", "image_url": "default", "width": "1270.59px", "height": "900px", "visible": "true" }, { "name": "shape-0", "type": "shape", "background-color": "black", "border-color": "initial", "border-width": "initial", "visible": "true" },
July 11, 2021 at 10:32 AM #29422Long Nguyen
ModeratorHi,
You can create your own function to get the number of modifications and items in each modification object and then use the loop to create the number of meta boxes. The field should be
text
to use for all cases. Just likeadd_filter( 'rwmb_meta_boxes', function() { $modifications = get_the_modification_and_items(); foreach ( $modifications as $index => $modification ) { $meta_boxes[] = [ .... ] } return $meta_boxes; } );
Hope that makes sense.
- Programmatically create Metabox.io custom fields based on the returned JSON key pairs.
-
AuthorPosts
- You must be logged in to reply to this topic.