Custom Fields
Re-Envisioned
Support General Dynamic Custom Fields Via JSON

  • Creator
    Topic
  • #29384
    TopDog
    Participant

    I 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 values

    The 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:

    1. 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?
    2. 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?"
Viewing 3 replies - 1 through 3 (of 3 total)
  • Author
    Replies
  • #29391
    Long Nguyen
    Moderator

    Hi,

    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?

    #29403
    TopDog
    Participant

    The 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"
            },
    
    #29422
    Long Nguyen
    Moderator

    Hi,

    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 like

    add_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.

Viewing 3 replies - 1 through 3 (of 3 total)
  • You must be logged in to reply to this topic.