2

i want to update a custom user_meta field when a User changed/edit the xProfile field (width the ID 1542).

this does hook not work

    function action_xprofile_data_after_save( $x )
    { 

        print_r($x);

    //    if($field == 1542)
    //    {
    //        update_user_meta($user_id, 'field_1542', 'changed');
    //    }
    }
    add_action( 'xprofile_data_after_save', 'action_xprofile_data_after_save', 10, 1 ); 
shanebp
  • 1,904
  • 3
  • 17
  • 28
Peter
  • 11,413
  • 31
  • 100
  • 152

2 Answers2

3

I believe this approach works for edits made on both front-end and back-end. And it provides the $user_id:

function peter_xprofile_data_after_save( $data ) {

    if ( $data->field_id == 1542 ) {

        update_user_meta( $data->user_id, 'field_1542', 'changed');

    }
}
add_action( 'xprofile_data_after_save', 'peter_xprofile_data_after_save' );
shanebp
  • 1,904
  • 3
  • 17
  • 28
0

The above works fine in case of an edit, but not working when you "clean / remove" the text from the field. You should use something like this:

function peter_xprofile_data_after_save( $data ) {

    $field_content = bp_get_member_profile_data('field=field_name'); // enter your field name here

    if($field_content == '') {
        update_user_meta( $data->user_id, 'field_1542', '' );
    }

    if ( $data->field_id == 1542 ) {
        update_user_meta( $data->user_id, 'field_1542', $data->value);
    }
}
add_action( 'xprofile_data_after_save', 'peter_xprofile_data_after_save' );
Ago-S
  • 1
  • 3