Skip to:
Content
Pages
Categories
Search
Top
Bottom

Function for returning profile data


  • Mr3k
    Participant

    @mr3k

    Hello,

    I know this is a common topic regarding using xprofile_get_field_data. However, I’ve spent way too many hour to get this to work. My goal is to have a function in my childtheme’s function.php that returns a users profile data. I’ve made a dummy function below that should bring out the profile data from the field Yrkesroll, from user 1 and present it in comma format. When the function is called, nothing happens..

    function bp_user_yrkes(){
    	$field = 'Yrkesroll'; 
    	  
    	// The ID of the user. 
    	$user_id = 1; 
    	  
    	// How should array data be returned? 'comma' if you want a 
    	// comma-separated string; 'array' if you want an array. 
    	$multi_format = 'comma'; 
    	  
    	// NOTICE! Understand what this does before running. 
    	$result = xprofile_get_field_data($field, $user_id, $multi_format); 
    	echo $result;
    
    }
    
    bp_user_yrkes();

    I also made another function:

    add_action( 'bp_ready', 'hisgen_check' );
    function hisgen_check() {	
       echo xprofile_get_field_data( 'Yrkesroll', 1, 'comma' );
    }
    hisgen_check();

    This works perfectly and echo the profile data, but as fast as I try to make that this function is returning the data instead of echo, this doesn’t work. It has something to do with “add_action( ‘bp_ready’, ‘hisgen_check’ );” I guess.

    Some guidelines here would be perfect. Thanks in advance!

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

  • shanebp
    Moderator

    @shanebp

    It works with the add_action because BP is ready then – iow. the function xprofile_get_field_data is available.

    In your other examples, BP has not yet loaded, so the function is not available.


    Mr3k
    Participant

    @mr3k

    Thanks @shanebp for the answer!

    I can understand this. But if I change to this code:

    add_action( 'bp_ready', 'hisgen_check' );
    function hisgen_check() {	
       return xprofile_get_field_data( 'Yrkesroll', 1, 'comma' );
    }
    echo hisgen_check();

    Would that work then? I’ve tried several combination of this without success..


    shanebp
    Moderator

    @shanebp

    The echo won’t work – it calls the xprofile function before it is available.
    You need to google add_action and learn what it does.


    Mr3k
    Participant

    @mr3k

    Thanks @shanebp for the guidance. I read some about add_action and manage to get the function to work as desired.

    For people interested, this is what I did. I created a custom function that follows:

    add_action( 'bp_ready', 'custom_function' );
    
    // The ID of the field, or the $name of the field. 
    function custom_function($user_id){
    	$field = 'fieldname'; 
    	  
    	// How should array data be returned? 'comma' if you want a 
    	// comma-separated string; 'array' if you want an array. 
    	$multi_format = 'comma'; 
    	  
    	// NOTICE! Understand what this does before running. 
    	$result = xprofile_get_field_data($field, $user_id, $multi_format); 
    	return $result;
    }

    The main function that I wanted to get the profile data was then called.

    Have a great day!

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