Skip to:
Content
Pages
Categories
Search
Top
Bottom

Getting the current logged in user ID?

  • @kriyou

    Participant

    This appears to be harder than I think! I thought I had this right, but for some reason it won’t output the user ID!

    Now the point of this function is to eventually declare that the user is already a part of one group, and as such can’t join any others. This is one of the main points of the site – incidentally, I’m surprised that I’ve been unable to find a plugin for this.

    Here’s the code:

    function one_group_only() {
    global $wpdb, $bp;

    $sql = “SELECT *, COUNT(user_id) FROM fn_bp_groups_members
    WHERE user_id = {$bp->loggedin_user->id}”;
    print_r($sql);
    die();
    }
    do_action( ‘one_group_only’ );
    one_group_only();

    So what this function does so far is tries to output the query to the database (that’s as far as I’ve gotten). The query that was output was this:

    SELECT *, COUNT(user_id) FROM fn_bp_groups_members WHERE user_id =

    And that’s it, I have no idea why it can’t recognise the user ID. It’s a little frustrating, as I think I’ve gotten it right. Called the correct $bp global, used the correct reference point to find the exact string, and my SQL output doesn’t cause any problems when I manually put the test user ID in.

    Any ideas?

Viewing 7 replies - 1 through 7 (of 7 total)
  • @djpaul

    Keymaster

    Where are you calling this code, how are you calling it, and you have checked that you’ve logged in as a user, right?

    @kriyou

    Participant

    1) At the bottom of bp-groups.php.
    2) In a function enclosed on its own (which is just below where the function code ends).
    3) Definitely, positively logged in as a user.

    @kriyou

    Participant

    Any ideas?

    @kriyou

    Participant

    And once again.

    Someone please help me with this.

    @peteranselmo

    Participant

    Is it tied to any action hook? PHP reads in all the BP code before executing much of it. If your function is called too early, the BP global won’t have been populated yet. That may or may not be the problem, but worth experimenting with.

    @nuprn1

    Participant

    if you just need to check if a user is already a member of “a” group – then use one of the built in functions:

    groups_get_user_groups( $bp->loggedin_user->id ) – returns list of ids
    or
    groups_total_groups_for_user( $bp->loggedin_user->id ) – returns a basic count which is wp_cached

    @kriyou

    Participant

    Rich, I love you. I’ll pass on the advice so that others might know how to do it.

    At the top of /groups/index.php, you will see a H3 tag, with the ‘Create Group’ button next to it. This has a conditional around it to show the button if the user is logged in – this needs slight modification to check if the user is already part of a group. Define it first, above that tag:

    $count = groups_total_groups_for_user( $bp->loggedin_user->id );

    Around the button, the conditional was previously just for the ‘is_logged_in’ check, but I modified it to this:

    if ( is_user_logged_in() && $count < 1

    I am happy with the results, thank you Rich.

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘Getting the current logged in user ID?’ is closed to new replies.
Skip to toolbar