Skip to:
Content
Pages
Categories
Search
Top
Bottom

bp_loggedin_user_id() is always 0


  • anika_r
    Participant

    @anika_r

    Hi I’m interested in writing a custom function to get the users xprofile data and parsing it. However i cannot get xprofile_get_field_data(‘1′,’1’) to work. When I run it I get an empty string.In fact I can’t even get bp_displayed_user_id(), bp_loggedin_user_id() or $bp->profile->table_name_data to work. They all give me a value of 0 because they are empty for some reason even if the id of the logged in user clearly isn’t 0. I know this from looking at the database and also by looking at get_current_user_id() which works fine.

    So they way I’ve structured it, is I am using the Tesseract theme which I downloaded. Then made a child theme and i am writing my functions in functions.php.

    When I tried putting my bp_displayed_user_id() and bp_loggedin_user_id() calls in bp-custom.php in /Applications/XAMPP/htdocs/wordpress/wp-content/plugins I don’t get any value at all. I don’t even get 0. So that means its not even running. Also when I put it in a custom plugin file in wp-content/plugins I get no value as well. I would really love any help!!!

    My ultimate aim is to take in xprofile data that a user enters in as a dare and then compare it to teh current date and then output this in the user’s profile.

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

  • shanebp
    Moderator

    @shanebp

    What hook are you using?

    Try:

    function anika_test() { 
        echo 'logged in ID: ' .  bp_loggedin_user_id();
        echo '<br>some field: ' .  xprofile_get_field_data('some field name', bp_loggedin_user_id() );
    }
    add_action( 'bp_ready', 'anika_test' );

    bp_displayed_user_id() needs to be called in the context of a profile.
    Look at the template files for an appropriate hook.


    anika_r
    Participant

    @anika_r

    Thank you soo much! That worked!! I wasn’t using a hook. I wasted so much time trying to figure that out! Next time I will ask much sooner!


    muskokee
    Participant

    @muskokee

    Hi @shanebp, I was wondering if I could put the “add_action” within a function and have it work? I have tried but I have been having so many problems this morning I just need to make sure that it is truly not possible! Here is my function derived from your function above:

    function is_right_user() { 
    global $bp;
    
            $loggedin =  bp_loggedin_user_id();
    	$displayed =  bp_displayed_user_id();
    
    	
    	if ( $loggedin == $displayed) {		
    		
    		add_action( 'bp_setup_nav', 'custom_bp_subnav', 50 );
    	}
    	
    }
    add_action( 'bp_ready', 'is_right_user' );

    I would like to do a check to make sure that the user is only on their own page when being able to access a subnav item.

    Thanks


    Henry Wright
    Moderator

    @henrywright

    You can use add_action() anywhere. Whether or not it executes the functions attached to it is another question. Take the add_action() inside your function for example. It’ll only execute custom_bp_subnav() on the condition that $loggedin == $displayed. Also the bp_ready hook must be running.


    shanebp
    Moderator

    @shanebp

    In addition to Henry’s comments…

    Don’t include the bp global when you don’t use it in the function.

    There is no advantage to your approach, and you expose yourself to a possible timing issues re the hooks. Just call the hook:

    function custom_bp_subnav() { 
    
            $loggedin =  bp_loggedin_user_id();
    	$displayed =  bp_displayed_user_id();
    
    	
    	if ( $loggedin == $displayed) {		
    		
    		//  do something
    	}
    	
    }
    add_action( 'bp_setup_nav', 'custom_bp_subnav', 50 );

    muskokee
    Participant

    @muskokee

    Thanks @shanebp. This is the approach I eventually went with and all is working as it should!

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘bp_loggedin_user_id() is always 0’ is closed to new replies.
Skip to toolbar