• Resolved Giulio

    (@ferocious)


    Hi, I have about sixty input fields to be collected with my CPT. For this purpose I’m using the meta boxes. I’d like know if there are data structures suitable to achieve this task. For example, BadgeOS does use of associative arrays of array for defining custom fields. So I’m also trying to define them in my code. Here some snippet codes:

    function custom_fields() {
    $prefix = 'var_';
    
    // First meta box
    $fields = array (
      array (
        'label' => 'Picture',
        'id' => $prefix . 'picture',
        'desc' => 'Description...',
        'type' => 'select',
        'options' => array ( 'Picture 1', 'Picture 2', 'Picture 3', )
       ),
      array (
         'label' => 'Something',
         ....
      ),
      ...
    );
    $cutsom_fields[] = array (
      'id' => 'first_meta_box',
      'title' => 'Title',
      'context' => 'side',
      'priority' => 'low',
      'page_slug' => 'mf_product',
      'fields' => $fields,
    };
    // Second meta box
    $fields = array ( ... );
    $custom_fields[] = array ( ... ),
    ...
    // And so forth
    ...
    return $custom_fields;
    }
    
    function create_mbox() {
     $cf = custom_fields();
     $cpt = array_column( $cf, 'page_slug' );
     foreach ( $cpt as $custom_post ) {
       foreach ( $cf as $field ) {
          add_meta_box( $field['id'],
          'print_mbox'',
          $custom_post,
          $field['context'],
          $field['priority'],
          $field['fields'] );
       }
     }
    }

    Is there a more efficient way than this?

Viewing 3 replies - 1 through 3 (of 3 total)
  • Moderator bcworkz

    (@bcworkz)

    It’d be slightly more efficient to directly make a series of add_meta_box() calls without first setting up definition arrays. Definition arrays typically help you organize your data and make code maintenance easier. It’s often worth the slight performance hit to do this.

    OTOH, it may be possible to nicely organize direct add_meta_box() calls that achieve the same thing without intermediate definition arrays. For the most part coding-wise, do whatever works best for you without worrying too much about efficiency, especially for back end screens that are not heavily used by hundreds of users.

    Where it does make sense to think about efficiency is your user interface design. Sixty fields are a lot for a CPT. Maybe there is no help for it, that’s what’s required. Consider presenting only the fields that are absolutely required in a meta box, off loading other fields to a separate admin page. Then all those fields do not need to be processed when loading the main edit screen.

    Consider not offering so many options if possible. As coders, we are aware of multitudes of possibilities, and generally like being able to fine tune things through such options. Most people could not care less. They don’t want to decide, they just want things to work well without needing to change anything. Only provide options that most people will actually use. You can install filter and action hooks to handle options most people wouldn’t care about. The few that do can still change things if they are so inclined.

    I’m speaking in general UI terms now, not specifically about meta boxes or CPTs. Since I’ve no idea what these fields are for, I can’t say how important any of them are and if the can be moved or removed. However, moving or removing fields will be much more efficient than if you used intermediate arrays or not.

    Thread Starter Giulio

    (@ferocious)

    Hello Bcworks, thank you for your reply.
    The CPT plays a central role in my plugin. I’m trying to develop a D&D character builder. Maybe I could use the Metadata API for storing the fields into WordPress after they have been serialized on plugin activation. Does it make sense? But the job gets too complicated.
    Thank you again.

    Moderator bcworkz

    (@bcworkz)

    Ah! Now the 60 fields make sense ??

    How the data is best stored depends on how you will be using it. Serialized data is much more efficient than storing individual values, but you cannot easily query using specific values if they are serialized, especially when using WP_Query methods. If you will be needing to query by these values I think your own custom table or even tables might be in order. This does throw out the possibility of using WP_Query for related queries, you need to rely on $wpdb object methods to get queries involving your custom table(s) done.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘What is the best data structure for meta boxes?’ is closed to new replies.