$bp global values changing when I move code to a class

I am creating my own subnav menu by looping over the bp_options_nav object inside the $bp global.

something along the lines of:

$subnavs = $bp->bp_options_nav[$parent];

I then check if user has access to it and if they do output it like this:

foreach ($subnavs as $item)
	{
		if ($item['user_has_access'])
		{
			// my output code
		}
	}

This works great when I add it directly into a template file like profile.php but I don't want to have it copied into every file, I want it to be centralised.

When I call my code on the template from a class like so:

mynamespace\budddyhelp::my_function('profile');

The $bp global now only shows I have access to public subnavs.

Inside my function I have tried doing:

global $bp

and

$bp = buddypress();

But its same story either way. I read that I shouldn't pass the $bp object as a reference parameter but I am a bit confused here. It seems to be changing values based on scope. I am not sure how that is happening.

How can I get the correct values of the $bp global inside my helper function?

  • Michael Bissett

    Hey @Mike, hope you're doing well today! :slight_smile:

    I'll admit that this is a bit outside of my realm here, but I can call in our SLS staff here, they're more savvy about this sort of thing.

    In the meantime though, could I ask you to post your helper function to pastebin.com, and share a link to it here? It'd help if we had the whole function available. :slight_smile:

    Thanks! :slight_smile:

    Kind Regards,
    Michael

    P.S. I should also note that your earlier code:

    mynamespacebudddyhelp::my_function('profile');

    Had 3 d's in buddy, is that how you have it in the code, or was that a mistype here on the thread? Curious as to whether that's part of the problem.

  • Mike

    Hey Michael,

    It's working for me now. I started out with the buddypress() method and then changed to global var method. I think buddypress() must re-initialise the object and it somehow got cached so when I switched to "global $bp" it was still pulling other one.

    I went back to code today to step through it again and it was working and I hadn't changed a single line so I guess it has to be cache.

    Sorry to take your time.

    (ps. yeah it was just a typo as I wrote ticket)

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.