Private groups Shortcodes

Below, you’ll find a detailed guide on how to add the Private groups Shortcodes to your WordPress website, including their parameters, examples, and PHP function code. Additionally, we’ll assist you with common issues that might cause the Private groups Plugin shortcodes not to show or not to work correctly.

Before starting, here is an overview of the Private groups Plugin and the shortcodes it provides:

Plugin Icon
Private groups

"Private Groups is a dynamic WordPress plugin with the slug bbp-private-groups. It provides robust functionality to create exclusive, password-protected communities within your website."

★★★★☆ (49) Active Installs: 2000+ Tested with: 6.2.3 PHP Version: false
Included Shortcodes:
  • [list-pg-users]
  • [pg-single-forum]
  • [pg-single-topic]
  • [pg-single-reply]

Private groups [list-pg-users] Shortcode

The bbp-private-groups plugin shortcode, ‘list-pg-users’, lists all users belonging to a specified group. If no group is specified, it lists all users. This shortcode retrieves users within a group, displaying their names. If the group isn’t defined, it shows all users.

Shortcode: [list-pg-users]

Examples and Usage

Basic example – The following shortcode lists all users within a specified private group. The ‘group’ attribute should be replaced with the actual name of the group.

[list-pg-users group="Group Name"]

Advanced examples

Displaying a list of users from a specific private group by referencing the group name. If the group name is not found, it will list all users.

[list-pg-users group="Group Name"]

Using the shortcode without any attributes will list all users, regardless of their private group.

[list-pg-users]

Note: In the shortcode [list-pg-users group=”Group Name”], replace “Group Name” with the actual name of the group you want to display users from.

PHP Function Code

In case you have difficulties debugging what causing issues with [list-pg-users] shortcode, check below the related PHP functions code.

Shortcode line:

add_shortcode('list-pg-users', 'list_pg_users');

Shortcode PHP function:

function list_pg_users ($attr) {
global $rpg_groups ;
$users= get_users() ;
	if ( !empty( $attr['group'] ) )  {
	//we have a group name !
	$content=$attr['group'] ;
	foreach ( $rpg_groups as $group => $details ){
		if ($details == $content) {
		echo '<b>'.$details.'</b>';
		echo '<ul>' ;
				foreach ( $users as $user ) {
				$groupcheck=get_user_meta( $user->ID, 'private_group',true);
				if ($groupcheck == $group) echo '<li>'.esc_html( $user->display_name ).'</li>' ;
				}
		echo '</ul>' ;
		}
	}
	}
	else {
	// we don't have a group name, so show all !
		foreach ( $rpg_groups as $group => $details ){
		if ($details == '') $details = $group.' - no name' ;
		echo '<b>'.$details.'</b>';
		echo '<ul>' ;
			foreach ( $users as $user ) {
			$groupcheck=get_user_meta( $user->ID, 'private_group',true);
			if ($groupcheck == $group) echo '<li>'.esc_html( $user->display_name ).'</li>' ;
			}
		echo '</ul>' ;
		}
	}
}

Code file location:

bbp-private-groups/bbp-private-groups/includes/shortcodes.php

Private groups [pg-single-forum] Shortcode

The ‘pg-single-forum’ shortcode from bbp-private-groups plugin displays a specific forum based on the ID passed. It checks if the forum exists and if the user has access. If not, a custom message is displayed.

Shortcode: [pg-single-forum]

Parameters

Here is a list of all possible pg-single-forum shortcode parameters and attributes:

  • id – unique identifier for the forum
  • message – a user-defined message displayed when access is denied

Examples and Usage

Basic example – The following shortcode is used to display a single forum with the ID of 1.

[pg-single-forum id=1 /]

Advanced examples

Display a single forum by ID and provide a custom message when the user does not have the capability to view the forum. The message will be displayed instead of the forum content.

[pg-single-forum id=1 message="Sorry, you do not have permission to view this forum." /]

Display multiple forums by using multiple shortcodes. Each shortcode will display a different forum based on the ID provided. If the user does not have the capability to view the forum, a custom message will be displayed.

[pg-single-forum id=1 message="Sorry, you do not have permission to view this forum." /]
[pg-single-forum id=2 message="Sorry, you do not have permission to view this forum." /]
[pg-single-forum id=3 message="Sorry, you do not have permission to view this forum." /]

PHP Function Code

In case you have difficulties debugging what causing issues with [pg-single-forum] shortcode, check below the related PHP functions code.

Shortcode line:

add_shortcode('pg-single-forum', 'pg_single_forum');

Shortcode PHP function:

function pg_single_forum( $attr, $content = '' ) {

		// Sanity check required info
		if ( !empty( $content ) || ( empty( $attr['id'] ) || !is_numeric( $attr['id'] ) ) )
			return $content;

		// Set passed attribute to $forum_id for clarity
		$forum_id = bbpress()->current_forum_id = $attr['id'];

		// Bail if ID passed is not a forum
		if ( !bbp_is_forum( $forum_id ) )
			return $content;

		// Start output buffer
		pg_start( 'bbp_single_forum' );
		
		//check if pg user cannot view forum
		if (!private_groups_can_user_view_post_id($forum_id)) {
			if (!empty($attr['message'])) {
			echo $attr['message'] ;
			}
		return pg_end() ;
		}
		

		// Check forum caps
		if ( bbp_user_can_view_forum( array( 'forum_id' => $forum_id ) ) ) {
			bbp_get_template_part( 'content',  'single-forum' );

		// Forum is private and user does not have caps
		} elseif ( bbp_is_forum_private( $forum_id, false ) ) {
			bbp_get_template_part( 'feedback', 'no-access'    );
		}

		// Return contents of output buffer
		return pg_end();
	}

Code file location:

bbp-private-groups/bbp-private-groups/includes/shortcodes.php

Private groups [pg-single-topic] Shortcode

The bbp-private-groups plugin shortcode ‘pg-single-topic’ is designed to display a specific forum topic. It first checks the required information, resets the global variables, and verifies if the ID passed is a topic. If the theme compatibility is not active, it resets the necessary attributes for the topic loop to function. The shortcode also checks if the user has the capability to view the forum. If the forum is private and the user lacks capabilities, a ‘no-access’ feedback is displayed.

Shortcode: [pg-single-topic]

Parameters

Here is a list of all possible pg-single-topic shortcode parameters and attributes:

  • id – Unique identifier of the topic to display
  • message – Custom message displayed when user can’t view post

Examples and Usage

Basic example – Displays a single topic using its ID

[pg-single-topic id=1 /]

Advanced examples

Displays a single topic using its ID but with a custom message if the user does not have the permission to view the topic.

[pg-single-topic id=1 message="Sorry, you do not have the permission to view this topic." /]

Displays a single topic using its ID but with a custom message if the user does not have the permission to view the topic. This example also uses a different language.

[pg-single-topic id=1 message="Lo siento, no tienes permiso para ver este tema." /]

PHP Function Code

In case you have difficulties debugging what causing issues with [pg-single-topic] shortcode, check below the related PHP functions code.

Shortcode line:

add_shortcode('pg-single-topic', 'pg_display_topic');

Shortcode PHP function:

function pg_display_topic( $attr, $content = '' ) {

		// Sanity check required info
		if ( !empty( $content ) || ( empty( $attr['id'] ) || !is_numeric( $attr['id'] ) ) )
			return $content;

		// Unset globals
		pg_unset_globals();

		// Set passed attribute to $forum_id for clarity
		$topic_id = bbpress()->current_topic_id = $attr['id'];
		$forum_id = bbp_get_topic_forum_id( $topic_id );

		// Bail if ID passed is not a topic
		if ( !bbp_is_topic( $topic_id ) )
			return $content;

		// Reset the queries if not in theme compat
		if ( !bbp_is_theme_compat_active() ) {

			$bbp = bbpress();

			// Reset necessary forum_query attributes for topics loop to function
			$bbp->forum_query->query_vars['post_type'] = bbp_get_forum_post_type();
			$bbp->forum_query->in_the_loop             = true;
			$bbp->forum_query->post                    = get_post( $forum_id );

			// Reset necessary topic_query attributes for topics loop to function
			$bbp->topic_query->query_vars['post_type'] = bbp_get_topic_post_type();
			$bbp->topic_query->in_the_loop             = true;
			$bbp->topic_query->post                    = get_post( $topic_id );
		}

		// Start output buffer
		pg_start( 'bbp_single_topic' );
		
		//check if pg user cannot view post
		$post_id = $attr['id'] ;
		$post_type = get_post_type( $post_id ) ;
		$forum_id = private_groups_get_forum_id_from_post_id($post_id, $post_type );
		if (!private_groups_can_user_view_post_id($forum_id) || topic_permissions_check ($post_id) == false) {
			if (!empty($attr['message'])) {
			echo $attr['message'] ;
			}
		return pg_end() ;
		}

		// Check forum caps
		if ( bbp_user_can_view_forum( array( 'forum_id' => $forum_id ) ) ) {
			bbp_get_template_part( 'content', 'single-topic' );

		// Forum is private and user does not have caps
		} elseif ( bbp_is_forum_private( $forum_id, false ) ) {
			bbp_get_template_part( 'feedback', 'no-access'    );
		}

		// Return contents of output buffer
		return pg_end();
}

Code file location:

bbp-private-groups/bbp-private-groups/includes/shortcodes.php

Private groups [pg-single-reply] Shortcode

The ‘pg-single-reply’ shortcode from the bbp-private-groups plugin allows displaying a single reply in a forum. It checks the validity of the reply ID, ensures the user has access, and returns the content.

Shortcode: [pg-single-reply]

Parameters

Here is a list of all possible pg-single-reply shortcode parameters and attributes:

  • id – numeric identifier assigned to a specific reply
  • message – text displayed when a user can’t view the post

Examples and Usage

Basic example – Displays a single reply from the bbPress forum using the reply’s ID.

[pg-single-reply id=123 /]

Advanced examples

Display a single reply from the bbPress forum using the reply’s ID, and show a custom message if the user does not have permission to view the post.

[pg-single-reply id=123 message="Sorry, you do not have permission to view this post." /]

Display a single reply from the bbPress forum using the reply’s ID, and show a different custom message if the user does not have permission to view the post.

[pg-single-reply id=456 message="Access denied. Please log in to view this post." /]

PHP Function Code

In case you have difficulties debugging what causing issues with [pg-single-reply] shortcode, check below the related PHP functions code.

Shortcode line:

add_shortcode('pg-single-reply', 'pg_display_reply');

Shortcode PHP function:

function pg_display_reply( $attr, $content = '' ) {

		// Sanity check required info
		if ( !empty( $content ) || ( empty( $attr['id'] ) || !is_numeric( $attr['id'] ) ) )
			return $content;

		// Unset globals
		pg_unset_globals();

		// Set passed attribute to $reply_id for clarity
		$reply_id = bbpress()->current_reply_id = $attr['id'];
		$forum_id = bbp_get_reply_forum_id( $reply_id );

		// Bail if ID passed is not a reply
		if ( !bbp_is_reply( $reply_id ) )
			return $content;

		// Reset the queries if not in theme compat
		if ( !bbp_is_theme_compat_active() ) {

			$bbp = bbpress();

			// Reset necessary forum_query attributes for replys loop to function
			$bbp->forum_query->query_vars['post_type'] = bbp_get_forum_post_type();
			$bbp->forum_query->in_the_loop             = true;
			$bbp->forum_query->post                    = get_post( $forum_id );

			// Reset necessary reply_query attributes for replys loop to function
			$bbp->reply_query->query_vars['post_type'] = bbp_get_reply_post_type();
			$bbp->reply_query->in_the_loop             = true;
			$bbp->reply_query->post                    = get_post( $reply_id );
		}

		// Start output buffer
		pg_start( 'bbp_single_reply' );
		
		//check if pg user cannot view post
		$post_type = get_post_type( $reply_id ) ;
		$forum_id = private_groups_get_forum_id_from_post_id($reply_id, $post_type );
		if (!private_groups_can_user_view_post_id($forum_id) || topic_permissions_check ($reply_id) == false) {
			if (!empty($attr['message'])) {
			echo $attr['message'] ;
			}
		return pg_end() ;
		}

		// Check forum caps
		if ( bbp_user_can_view_forum( array( 'forum_id' => $forum_id ) ) ) {
			bbp_get_template_part( 'content',  'single-reply' );

		// Forum is private and user does not have caps
		} elseif ( bbp_is_forum_private( $forum_id, false ) ) {
			bbp_get_template_part( 'feedback', 'no-access'    );
		}

		// Return contents of output buffer
		return pg_end();
	}

Code file location:

bbp-private-groups/bbp-private-groups/includes/shortcodes.php

Conclusion

Now that you’ve learned how to embed the Private groups Plugin shortcodes, understood the parameters, and seen code examples, it’s easy to use and debug any issue that might cause it to ‘not work’. If you still have difficulties with it, don’t hesitate to leave a comment below.

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *