Skip to:
Content
Pages
Categories
Search
Top
Bottom

Updating extended profile field (checkbox data)


  • BobSD99
    Participant

    @bobsd99

    I have created an extended profile field called “correspondence” with a checkbox data type, and the only option is “Subscribe to Free Newsletter.”

    It is showing up properly on the registration form, so when users come in, if they select it, their extended profile is reflecting the data. I’m trying to create a form and function to update this setting, and I’m having difficulty.

    My goal is to both understand the data storage and processes, as well as properly manipulate it. In PHPMyAdmin, in the table wp_bp_xprofile_fields, I see the fields:

    ID 307 “Correspondence”
    ID 315 “Subscribe to Free Newsletter”

    It appears BuddyPress is storing the user’s setting in the table wp_bp_xprofile_data. One user’s entry follows:

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

    So I see the data has been serialized and stored as field_id 307. I can thus access this field with the following function:

    $data = xprofile_get_field_data( 307, $userID );
    
    echo var_dump($data) . "<br>\n"; 
    // array(1) {
    //   [0]=>
    //   string(28) "Subscribe to Free Newsletter"
    // }
    
    echo "Setting is $data[0]<br>\n"; // "Subscribe to Free Newsletter"

    So far, so good – I can test for this field. But when I try to update it, I’m running into trouble:

    xprofile_set_field_data( 307, $userID, "Subscribe to Newsletter Digest" );

    Contrary to expectations, this doesn’t seem to alter that field at all.

    I tried updating the value with an array:

    $new_value = array( 'Subscribe to Newsletter Digest' );
    xprofile_set_field_data( 307, $userID, $new_value );

    No change. What am I doing wrong here?

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

  • BobSD99
    Participant

    @bobsd99

    To keep this simple, and for illustration purpose, I edited the data I wanted to update in the question, which given it’s a checkbox, is confusing.

    I actually want to toggle that element off, so I assume I’d actually either a) update the array to an empty field, or b) delete the entry entirely.

    I assume I want to change the field to an empty string, but now that doesn’t seem as obvious. Some guidance is appreciated on updating this field and any curves a checkbox might throw in working with BP XProfile fields.


    shanebp
    Moderator

    @shanebp

    It won’t work unless you pass a ‘valid’ value. And you don’t have to use an array for a single value.
    So if you have not changed the option value ( via Users > Profile Fields ) to ‘Subscribe to Newsletter Digest’, it will fail.

    To ‘uncheck’ the box – just delete that row for that user in the _bp_xprofile_data table.
    xprofile_delete_field_data( field name or ID, $user_id )


    BobSD99
    Participant

    @bobsd99

    So simple! Not obvious, though. I couldn’t even find xprofile_set_field_data documented in the Codex (although xprofile_get_field_data is). Thanks Shane, not the first time you’ve dug me out :o)


    BobSD99
    Participant

    @bobsd99

    A quick followup, I see the permitted field value for the checkbox (in this case) is stored in field 315 in table wp_bp_xprofile_fields – how do I query this checkbox setting value?

    I want to pull the correct value from the database, so in case I update that checkbox field’s option in the backend, I can correctly set it.

    I don’t see a function in /buddypress/bp-xprofile/bp-xprofile-functions.php: that seems to pull this, and nothing is locking in with Google….


    BobSD99
    Participant

    @bobsd99

    Found it, I think:

    $field = xprofile_get_field( 315 );
    echo "Field name: " . $field->name; 
    // Field name: Subscribe to Free Newsletter
Viewing 5 replies - 1 through 5 (of 5 total)
  • You must be logged in to reply to this topic.
Skip to toolbar