简体   繁体   中英

Update Buddypress user’s xprofile input

I'd like to update what users insert into 3 specific xprofile fields and store it to the database using sanitize_key (force letters lowercase, remove special characters besides "-" and "_"). The values change the way I want to temporarily when I echo them out, but don't store in the database.

Would really appreciate your help! Here's what I have so far

In my functions.php:

function expertise_tag_functions_before_save() {
  global $bp;
    foreach ($_REQUEST as $field => $value) {
        if ($field == ‘field_24’ || $field == ‘field_26’ || $field == ‘field_27’) {
            $value = sanitize_key( $value );
            $field_label = str_replace(‘field_’, ”, $field);
            xprofile_set_field_data($field_label, $user_id, $value);
    }
  }
};

add_action( ‘xprofile_data_before_save’, ‘expertise_tag_functions_before_save’, 10);
  • I've tried switching out 'xprofile_data_before_save for 'xprofile_data_after_save' but it still doesn't work.

Your changes are probably being overwritten after the filter hook runs. You are directly updating the field. You should be changing the submitted profile data. This is the filter hook which includes access to that data:

do_action_ref_array( 'xprofile_data_before_save', array( $this ) );

So try:

function expertise_tag_functions_before_save( $data ) {

   // make your changes to field values in $data

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM