Support

Account

Home Forums General Issues Enter Serialized Data

Solved

Enter Serialized Data

  • I have run into a situation where I need to enter serialized data into a text field.

    When saved into the database the data is double serialized.

    When the data is retrieved and displayed again by ACF it is completely unserialized, this causes some data to be missing (Arrays, ture/false, null) while other data becomes plain text strings.

    I need to avoid both of these conditions. Anyone have an idea how I can do this?

  • Well, this was not so much a problem with ACF as working around the need of WP to automatically unserialze and double/triple/quadruple serialize values.

    I needed to add a filter to the acf/load_value hook for the field in question, get the value of the field directly using get_post_meta() and then make sure it was formatted correctly before returning it to ACF.

  • John Huebner,
    Could you please provide the code that was your solution?

  • This was more than 6 years ago. I don’t even remember what I was working on that I needed to do this.

  • See Screenshot of the field form I added with ACF for the field _catcbll_btn_label. It’s a plugin-specific field, but I added it as a custom field entry in the ACF plugin, so that I would have an form field to enter data in the product screen.

    In the product post screen, I need to save this value a:1:{i:0;s:22:"Request Shipping Quote";} in the custom field _catcbll_btn_label to cause a plugin known as Custom Add to Cart Button Label and Link to display a shipping quote button. Yet the ACF or WP is not handling it correctly, as the _catcbll_btn_label field shows [""] when I refresh the product page after submitting the value a:1:{i:0;s:22:"Request Shipping Quote";}. Can you suggest anything helpful?

  • All I can really give you is what I posted about this subject when I was attempting to do it.

    You need to create an acf/load_value filter priority > 10. At this point the ACF value might be the unserialized version of the value, you will need to test this by var_dump($value). You might be able to correct the value at this point. If not then you need to use get_post_meta() to have WP get the value. This value will also be the unserialized value. WP automatically unserializes data when it is retrieved. Depending on the values that you get you then need to serialize it correctly so it matches what you want it to be and return the correct value from the filter.

    You’ve basically got to undo what WP has done to the value so that it appears in the text field correctly.

  • I have no idea what I’m doing. You mean something like this?

    function custom_unserialize($string){
    	  try {
            $reformatted = unserialize($string);
        } catch (\Exception $e) {
            return false;
        }
    
        
    	return $reformatted;
    }
    
    add_filter( 'acf/load_value/name=_catcbll_btn_label', 'custom_unserialize', 10 );
Viewing 7 posts - 1 through 7 (of 7 total)

The topic ‘Enter Serialized Data’ is closed to new replies.