• Resolved nickk40

    (@nickk40)


    I’m new to the Asgaros plugin and trying to write some custom code. For this, I need to check whether the currently logged-in user belongs to an Asgaros usergroup or not (to be precise: to *any* Asgaros usergroup, so not the user belongs to a *specific* group). Is there a function available to do this?

Viewing 2 replies - 1 through 2 (of 2 total)
  • Plugin Contributor Dominik Rauch

    (@qualmy91)

    Hey @nickk40,

    the follow lines should should work:

    
    // Get usergroups of current users 
    $usergroups = AsgarosForumUserGroups::getUserGroupsOfUser(get_current_user_id(), 'all', true);
    
    if($usergroups){
        // do something if user is in any usergroup
    }
    
    Thread Starter nickk40

    (@nickk40)

    Thanks, works perfectly! For anyone else interested: if you have a menu with the forum as one of the menu items, but the forum is only meant for users that are in any of the usergroups, then you can hide the forum from the menu for other users with this code:

    add_action( 'wp_head', function () { 
    	$current_user_id = get_current_user_id();
    	// Get usergroups of current users 
    	$usergroups = AsgarosForumUserGroups::getUserGroupsOfUser($current_user_id, 'all', true);
    
    	if (
    		!$current_user_id //not logged in
    		|| !$usergroups //not in any Asgaros usergroup
    	) {
    		?><style>.forum-item {display:none!important;}</style><?php	
    	}
    });

    This code assumes you have given the forum in the menu the class forum-item.

    • This reply was modified 4 years ago by nickk40.
Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Is there a function to check if logged-in user belongs to a usergroup?’ is closed to new replies.