Skip to:
Content
Pages
Categories
Search
Top
Bottom

Can functions in bp_template_tags.php be applied to a specific user ID?


  • dwdutch
    Participant

    @dwdutch

    i found a number of bp_* functions that may be useful for my programming attempt to generate information about members; HOWEVER, many of the functions do not accept input parameters so it seems like they can be applied only on the currently active (i.e. logged-in) user ID.

    Is there a way to use these functions on specific user IDs (e.g. while looping through members)?

    Am i missing some fundamental understanding of BP code execution? (I’ve scoured the documentation and forums but, so far, have not found any relevant reading material)

Viewing 9 replies - 1 through 9 (of 9 total)
  • As a rule of thumb, if something is in a -templatetags.php file, then it was designed to be used somewhere on a theme template. And if it isn’t in -templatetags.php, then it probably shouldn’t be used on a theme template.

    Which functions are you looking at?


    Boone Gorges
    Keymaster

    @boonebgorges

    As Paul said, those functions are intended to be used in a theme template. More specifically: they’re meant to be used inside of a member/group/activity/etc loop. So if you’re inside of a member loop

    functions like bp_member_name() will automatically detect which member is currently being processed. There’s no need for a $user_id argument.

    Most of the bp_has_x functions take arguments for specific members. For instance, bp_has_members( ‘include=3,7,24’ ) will make a loop of just those three members. Inside of that loop you can use any of the templatetags functions.

    See, for example, https://codex.buddypress.org/developer-docs/custom-buddypress-loops/the-members-loop/


    dwdutch
    Participant

    @dwdutch

    @DJPaul: I was looking for functions that might manipulate various xProfile fields (in particular) i was trying to extract info about checkbox type field. (I see that you may have provided me some insight on that topic in a separate thread).

    @boonebgorges: thanks for the clarification re: files used for themes – i was wondering why it was named as it was but i couldn’t find any explanation. I had looked at the members loop documentation and I saw that the while loop is controlled by bp_members() but since i could find no documentation on that function, I assumed that it must loop through all members on a site (which is overkill for my goal — i just need to identify a handful of members who have a certain xprofile value set THEN i want to display them).

    Meanwhile, back to this question… and my feeble attempt to avoid confusion.

    1) if I’m understanding you correctly then by calling bp_has_members( ‘include=3,7,24? ) I’m effectively setting up a “global” loop within my instantiation of BP that is incremented with each call to bp_members(). So, while within the “global” loop, all function call potentially reference the same current iteration of $user_id. Did i understand correctly?

    2) Yet, there must be certain functions that only refer to the currently logged-in user_id (e.g. is_site_admin()). How do i distinguish between which function type is which kind?

    3) Why can’t I find bp_has_members in http://bp-dev.org/phpxref/nav.html?_functions/index.html? Is there some other “official” place to go learn what functions exist and are available to me?


    dwdutch
    Participant

    @dwdutch

    Just to clarify… here’s what I’m doing. I’ve defined two xProfile groups: “Admin-only” and “Professional”. Next, I defined a membership-type field in that “Admin-only” group with values Pro, Standard and Volunteer. Finally, within the “Professional” group, I’ve provided several fields available only to that membership-type.

    Within my theme, I’ve modified edit.php to not display the SAVE CHANGES button on “Admin-only” edit-profile page unless user is a site administrator and to only display it on the “Professional” edit-profile page only if the user’s membership-type field (from the Admin-only” group) is set to Pro.

    Now, I’m working on a plug-in (because i don’t know how to add a page as part of a theme) that adds a unique page on my site that will list only those members who are membership-type=Pro. I want to display information from their respective Professional xProfile group fields. Eventually, I want to add filter capabilities to the top of the page based on the Professional xProfile group fields


    r-a-y
    Keymaster

    @r-a-y

    2) global $bp; echo $bp->loggedin_user->id; OR is_user_logged_in()

    3) Use my preferred method, dive into the code yourself! bp_has_members is available in /bp-core/bp-core-templatetags.php


    Boone Gorges
    Keymaster

    @boonebgorges

    @dwdutch
    1) Exactly right.

    2) Most (all?) BP-specific functions that default to the logged in user are named in a way that makes it clear. bp_loggedin_user_domain() is an example. There are some WP functions that take the logged in user as a default argument, and they’re generally not so helpfully named (fwiw the context is generally not so complex on a blog as it is in a BP network). current_user_can() is an example. When in doubt, I do a quick search through the codebase of a local installation. cd to the correct directory, and then grep -nR "function current_user_can" ./, etc.

    While the functions in bp-x-templatetags.php generally don’t accept arguments in the normal way – they get their arguments from the context of the iterating bp_has_x loop – there are also some functions that take user ids (or group ids, or activity ids, etc) as arguments. For instance, have a look at bp-groups.php and groups_get_total_member_count() – this isn’t intended to be used in the context of a groups loop. Generally you’re going to be better off using a loop, as the api is really designed around this kind of use, but you can use these other functions to do some things without loops.

    3) I used to use that phpxref on bp-dev.org all the time, but sadly it’s a version or two out of date, and is kind of useless at this point. (bp_has_members, for instance, used to be bp_has_site_members or something like that). If I had some spare time and bandwidth, I would set up an up-to-date phpxref of the current version of BP. In the meantime, I recommend following @r-a-y ‘s advice and going directly to the BP codebase, until the community has come up with a better solution


    dwdutch
    Participant

    @dwdutch

    I tried the loop… this is AWESOME!!

    @boonebgorges: I agree with @r-a-y ’s advice — of course, without knowing the software structure of BP, it’s harder to find specific functions. Still… understanding the underlying concept of the “global” loop has freed my thought-processes. I had read the documentation on the Loop but didn’t grasp that this was such a basic underlying concept.

    Thanks, guys!


    Boone Gorges
    Keymaster

    @boonebgorges

    Very glad it works for you! The loop can indeed be very awesome when you wrap your mind around it!


    dunc
    Participant

    @dunc

    @dwdutch Do you mind sharing your plug-in code? I’m trying to do pretty much the same thing – list members on a page who have have a certain profile field set.

    I plan on rolling out similar filtering elsewhere but at the moment that seems like a distant dream. ;)

    I am using your ‘Admin-only’ xProfile Group mod so it’s a double thanks from me.

Viewing 9 replies - 1 through 9 (of 9 total)
  • The topic ‘Can functions in bp_template_tags.php be applied to a specific user ID?’ is closed to new replies.
Skip to toolbar