Support

Account

Home Forums General Issues Convert JS serialized post-edit form data to get_fields array

Helping

Convert JS serialized post-edit form data to get_fields array

  • So, here’s something that I ran into and I’m not sure how to approach.

    Backstory.
    We’re creating an admin feature for Custom Post Type, let’s call it “A”, that should recalculate some data when we edit values on the edit form of said CPT. In order to perform that recalculation, we need information from CPT “B” that is not available on that edit screen.

    The recalculation should be possible to perform, even without actually saving the “A” edit form. We need to send current data to the backend, perform calculations, and return results for review before the user decides to Update the CTP for real.

    Idea.
    We know that we can run JS that will serialize edit post data, eg:
    jQuery(‘form’).serializeArray()

    That gives me the form data in the format of:
    JS serialized form

    That’s great, but parsing that would be a big pain in the …

    So, WP and ACF must be parsing this somehow already, in order to save it to the object. I wonder if there’s a good way to convert this serialized form to format that is returned by get_field or get_fields

    Toughts?

  • When you submit a form with ACF fields on it ACF reads through the $_POST[‘acf’] input recursively. It does not convert anything. This is just to give you an idea of what’s going on, this is not exact, just pseudocode.

    
    function recursive_function($array) {
      // not really the function name
      foreach ($array as $key => $value) {
        if (is_array($value)) {
          // recurse
          recursive_function($value);
        } else {
          // update field value
        }
      }
    }
    

    Obviously there is a whole lot more going. ACF looks up the field key to find the field type and the recursive calls are done by field types that allow sub fields and the data for each field is formatted correctly for each field type before updating.

    You would need to build a similar process.

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

You must be logged in to reply to this topic.