0

I'm working on some new project using BuddyPress, and one of the core features requires some information from a custom profile field. This field is a dropdown, so I only have 4 possible results. So well, it looked really simple to get this field, but I can't get it to work. I'm using the following code to get this field:

// If the user isn't logged in, just return 4 (default styling)
if (!is_user_logged_in()) {
return 4;
}
$user = wp_get_current_user();
$user_id = $user->id;
$user_team = xprofile_get_field_data( 3, 1 );

In this code, the field ID is fixed to 3 and the user ID is fixed to 1 (my user ID). Originally I used $user_id for the user ID, but this didn't work.

The problem with this code is, $user_team just returns string(0) "" if I run the code with a var_dump. Then, if I go and check the field in my profile in the backend, it's not filled in. However, when I don't run this code, the correct value is selected. I also can't change the value while this code is active.

Does someone know how to fix this (probably very easy) issue? I've already used bp_get_profile_field_data(), but this gave the exact same result.

Thank you in advance :).

  • Hi there, welcome to WPSE. As a third-party plugin Buddypress is off-topic here - we don't have the required knowledge to really help you thoroughly with this. You might want to try asking at the Buddypress forums instead. – Tim Malone Aug 4 '16 at 21:12
  • Sorry, I thought it was made by the WordPress team. At least their site is linked to the WordPress.org site, so I assumed it was made by them as well. – R Hisgen Aug 4 '16 at 21:44
0

It's probably due to the context of your code - which you didn't explain.

Try this in your-theme/functions.php

function hisgen_check() {
   echo 'field: ' . xprofile_get_field_data( 3, bp_loggedin_user_id(), 'comma' );
}
add_action( 'bp_ready', 'hisgen_check' );
  • Thank you! That code did it. The code I posted was the code I had in a function, and I then called that function directly. The only thing I don't want it to, is to echo it. I want to use this function for other parts of my theme, like getting a different stylesheet depending on the entered value. I think it didn't work because I didn't use a hook. Thanks again! :) – R Hisgen Aug 4 '16 at 22:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.