Skip to:

reconciling updated data in xprofile field stored as serialized array on sign-up

  • BobSD99


    I have an xprofile field for newsletter when a user registers, which stores the field as a serialized entry of the allowed value for that field, which is the name set in the BuddyPress back end, in this case:

    “Subscribe to free newsletter”

    This results in a serialized entry in the wp_bp_xprofile_data table (in my case, for field 307) that looks like this:

    id: 14421
    field_id: 307
    user_id: 15201
    value: a:1:{i:0;s:28:”Subscribe to Free Newsletter”;}

    However, when I use the BuddyPress function to update that data:

    // get permitted value for field 307 - required for xprofile_set_field_data() to work
    $field = xprofile_get_field( 315 );
    $UpdateField =  $field->name;
    xprofile_set_field_data( 307, $current_user->ID, $UpdateField );

    The result is the field is now set to a simple string:

    id: 14421
    field_id: 307
    user_id: 15201
    value: Subscribe to Free Newsletter

    I want to reconcile these entries, so they are the same format, but when I converted the string to a serialized field the update function fails:

    // serializing field $UpdateField
    $newfield = serialize ( $UpdateField );  // result: [s:28:"Subscribe to Free Newsletter";]
    xprofile_set_field_data( 307, $current_user->ID, $newfield );

    This command fails, which I believe is because the field must match the allowed value as set in the back-end for the extended profile. I suspect because the serialized field looks like this:

    s:28:"Subscribe to Free Newsletter";

    But maybe should look like this:

    a:1:{i:0;s:28:"Subscribe to Free Newsletter";}

    So maybe I can convert the datum to a properly serialized array with the correct argument to the serialized function, and then pass it to the xprofile_set_field_data() function and get it to update.

    But why is this behavior happening? This wouldn’t seem to be the expected behavior when I pass a string to the function. I would expect the value I set on registration would be the same format of the value set when I update the field. Am I doing something wrong and how do I reconcile these entries?

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

  • BobSD99


    Note: there is no transcription error, the field 315 references the allowed values for field 307



    WP and BP will serialize, if necessary, any user_meta or xprofile data.
    If you try to save a serialized array, it will treat it as a string and add slashes.

    Is $field->name a serialized array? Dump it and find out.
    Try some other values ( array, string, int ) and see how xprofile_set_field_data handles it.

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