Skip to:
Content
Pages
Categories
Search
Top
Bottom

Get xprofile field value in functions.php

  • Avatar of vince_
    vince_
    Participant

    @vince_

    Hi Guys !

    I need to get the value of the “Role” xprofile field of the current logged in user in the functions.php file (It’s located in bp-themes\bp-default\)

    I tried the following stuff :

    $role = xprofile_get_field_data(“Role”, bp_get_group_member_id());

    AND

    $role = bp_get_profile_field_data(
    array(
    ‘field’ => “Role”, // where 1 is the field_id or it can be the field name
    ‘user_id’ => bp_get_group_member_id()
    )
    );

    But none is working. Do you have any ideas ?

    Thanks

Viewing 9 replies - 1 through 9 (of 9 total)
  • Avatar of Henry
    Henry
    Participant

    @henrywright-1

    $args = array(
    'field'   => 'Role',
    'user_id' => bp_loggedin_user_id()
    );
    
    $role = bp_get_profile_field_data( $args );

    …should be what you’re after.

    Avatar of vince_
    vince_
    Participant

    @vince_

    Hi,

    No, it’s not working. My 2 other techniques work in other pages but not in functions.php.
    Does this file have something special ?

    Thanks

    Avatar of Henry
    Henry
    Participant

    @henrywright-1

    try adding this to the first line of your function in functions.php

    global $bp;

    Avatar of vince_
    vince_
    Participant

    @vince_

    It’s not working, here is my entire code (without the “if” condition it’s working) :

    $args = array(
    		'field'   => 'Role',
    		'user_id' => bp_loggedin_user_id()
    	);
    	
    	$role = bp_get_profile_field_data($args);
    	add_action( 'init', 'my_em_setup_nav' );
    	
    	function my_em_setup_nav(){
    		global $bp;	
    		if( $role == "Role1" ){
    			bp_core_remove_subnav_item('events','my-events');
    			bp_core_remove_subnav_item('events','my-locations');
    			bp_core_remove_subnav_item('events','my-bookings');
    		}	
    	}
    Avatar of vince_
    vince_
    Participant

    @vince_

    bp_loggedin_user_id() returns 0

    Avatar of vince_
    vince_
    Participant

    @vince_

    no sorry, it returns the right number

    Avatar of vince_
    vince_
    Participant

    @vince_

    I’ve got it … It wasn’t in the right order … I feel stupid

    add_action( ‘init’, ‘my_em_setup_nav’ );

    function my_em_setup_nav(){
    global $bp;
    $args = array(
    ‘field’ => ‘Role’,
    ‘user_id’ => bp_loggedin_user_id()
    );
    $role = bp_get_profile_field_data($args);
    if( $role == “Role1″ ){
    bp_core_remove_subnav_item(‘events’,’my-events’);
    bp_core_remove_subnav_item(‘events’,’my-locations’);
    bp_core_remove_subnav_item(‘events’,’my-bookings’);
    }
    }

    Avatar of Henry
    Henry
    Participant

    @henrywright-1

    Try putting everything except the add_action( 'init', 'my_em_setup_nav' ); inside the my_em_setup_nav() function. So

    function my_em_setup_nav(){
            global $bp;
            
            $args = array(
    		'field'   => 'Role',
    		'user_id' => bp_loggedin_user_id()
    	);
    	$role = bp_get_profile_field_data($args);
    
    	if( $role == 'Role1' ) {
    		bp_core_remove_subnav_item('events','my-events');
    		bp_core_remove_subnav_item('events','my-locations');
    		bp_core_remove_subnav_item('events','my-bookings');
    	}	
    }
    add_action( 'init', 'my_em_setup_nav' );
    Avatar of Henry
    Henry
    Participant

    @henrywright-1

    Didn’t see your last post…

    Brilliant! Glad you got it working :}

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

You must be logged in to reply to this topic.