Below, you’ll find a detailed guide on how to add the 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 Groups Plugin shortcodes not to show or not to work correctly.
Before starting, here is an overview of the Groups Plugin and the shortcodes it provides:
"Groups is a WordPress plugin that offers efficient group-based user memberships, content access control, and group capabilities management. Ideal for community-driven sites."
- [groups_member]
- [groups_non_member]
- [groups_can]
- [groups_can_not]
- [groups_login]
- [groups_logout]
- [groups_group_info]
- [groups_user_groups]
- [groups_groups]
- [groups_join]
- [groups_leave]
Groups [groups_member] Shortcode
The Groups Member shortcode checks if the current user is a member of a specified group and displays the content accordingly. It uses the Groups_User and Groups_Group classes to verify membership. If the user is part of the group, the content within the shortcode is displayed. If not, the output remains empty.
Shortcode: [groups_member]
Parameters
Here is a list of all possible groups_member shortcode parameters and attributes:
group
– Name or ID of the group to check membership for
Examples and Usage
Basic example – A simple usage of the groups_member shortcode to check if the current user is a member of a specific group.
[groups_member group="GroupName" /]
Here, we replace “GroupName” with the name of the group we want to check. If the current user is a member of this group, the content inside the shortcode will be displayed.
Advanced examples
Using the groups_member shortcode to check if the current user is a member of multiple groups.
[groups_member group="GroupName1,GroupName2,GroupName3" /]
In this example, we are checking if the current user is a member of any of the groups “GroupName1”, “GroupName2”, or “GroupName3”. We separate each group name with a comma. If the user is a member of any of these groups, the content inside the shortcode will be displayed.
Another advanced usage is to nest the groups_member shortcode inside another shortcode.
[some_shortcode][groups_member group="GroupName" /][/some_shortcode]
Here, we are nesting the groups_member shortcode inside the some_shortcode. The groups_member shortcode will first check if the current user is a member of “GroupName”. If the user is a member, the content inside the groups_member shortcode will be processed by the some_shortcode.
PHP Function Code
In case you have difficulties debugging what causing issues with [groups_member]
shortcode, check below the related PHP functions code.
Shortcode line:
add_shortcode( 'groups_member', array( __CLASS__, 'groups_member' ) );
Shortcode PHP function:
function groups_member( $atts, $content = null ) {
$output = '';
$options = shortcode_atts( array( 'group' => '' ), $atts );
$show_content = false;
if ( $content !== null ) {
$groups_user = new Groups_User( get_current_user_id() );
$groups = explode( ',', $options['group'] );
foreach ( $groups as $group ) {
$group = addslashes( trim( $group ) );
$current_group = Groups_Group::read( $group );
if ( !$current_group ) {
$current_group = Groups_Group::read_by_name( $group );
}
if ( $current_group ) {
if ( Groups_User_Group::read( $groups_user->user->ID , $current_group->group_id ) ) {
$show_content = true;
break;
}
}
}
if ( $show_content ) {
remove_shortcode( 'groups_member' );
$content = do_shortcode( $content );
add_shortcode( 'groups_member', array( __CLASS__, 'groups_member' ) );
$output = $content;
}
}
return $output;
}
Code file location:
groups/groups/lib/access/class-groups-access-shortcodes.php
Groups [groups_non_member] Shortcode
The ‘groups_non_member’ shortcode is used to control content visibility based on user group membership in WordPress. It checks if the current user is not a member of a specified group(s). If true, the enclosed content is displayed. If the user is a member, the content remains hidden. This shortcode provides a simple way to customize user experience based on group membership.
Shortcode: [groups_non_member]
Parameters
Here is a list of all possible groups_non_member shortcode parameters and attributes:
group
– Specifies the group(s) to check non-membership for
Examples and Usage
Basic example – Displays content only to non-members of a specific group.
[groups_non_member group="group1"]Your content here[/groups_non_member]
Advanced example – Displays content only to non-members of multiple groups. If the user is a member of any of the specified groups, the content will not be displayed to them.
[groups_non_member group="group1,group2,group3"]Your content here[/groups_non_member]
In both examples, replace “Your content here” with the content you want to display to non-members. Also, replace “group1”, “group2”, etc., with the actual names of your groups.
Note: If a group name contains spaces, enclose the name in double quotes. For example:
[groups_non_member group="group1,group2,\"group three\""]Your content here[/groups_non_member]
PHP Function Code
In case you have difficulties debugging what causing issues with [groups_non_member]
shortcode, check below the related PHP functions code.
Shortcode line:
add_shortcode( 'groups_non_member', array( __CLASS__, 'groups_non_member' ) );
Shortcode PHP function:
function groups_non_member( $atts, $content = null ) {
$output = '';
$options = shortcode_atts( array( 'group' => '' ), $atts );
$show_content = true;
if ( $content !== null ) {
$groups_user = new Groups_User( get_current_user_id() );
$groups = explode( ',', $options['group'] );
foreach ( $groups as $group ) {
$group = addslashes( trim( $group ) );
$current_group = Groups_Group::read( $group );
if ( !$current_group ) {
$current_group = Groups_Group::read_by_name( $group );
}
if ( $current_group ) {
if ( Groups_User_Group::read( $groups_user->user->ID , $current_group->group_id ) ) {
$show_content = false;
break;
}
}
}
if ( $show_content ) {
remove_shortcode( 'groups_non_member' );
$content = do_shortcode( $content );
add_shortcode( 'groups_non_member', array( __CLASS__, 'groups_non_member' ) );
$output = $content;
}
}
return $output;
}
Code file location:
groups/groups/lib/access/class-groups-access-shortcodes.php
Groups [groups_can] Shortcode
The ‘groups_can’ shortcode checks if a user has certain capabilities. If true, it displays the enclosed content. It uses the ‘Groups_User’ class to retrieve the current user’s capabilities.
Shortcode: [groups_can]
Parameters
Here is a list of all possible groups_can shortcode parameters and attributes:
capability
– Specifies user’s capabilities to access the content
Examples and Usage
Basic example – A simple usage of the ‘groups_can’ shortcode to check if the current user has the ‘editor’ capability.
[groups_can capability="editor"]You have the editor capability![/groups_can]
Advanced examples
Using the ‘groups_can’ shortcode to check if the current user has either ‘editor’ or ‘administrator’ capabilities. If the user has either of these capabilities, the enclosed content will be displayed.
[groups_can capability="editor,administrator"]You have either editor or administrator capability![/groups_can]
Another advanced usage of the ‘groups_can’ shortcode where we check for multiple capabilities. Here, the shortcode checks if the current user has the ‘editor’, ‘administrator’, or ‘author’ capabilities. If the user has any of these capabilities, the enclosed content will be displayed.
[groups_can capability="editor,administrator,author"]You have either editor, administrator, or author capability![/groups_can]
PHP Function Code
In case you have difficulties debugging what causing issues with [groups_can]
shortcode, check below the related PHP functions code.
Shortcode line:
add_shortcode( 'groups_can', array( __CLASS__, 'groups_can' ) );
Shortcode PHP function:
function groups_can( $atts, $content = null ) {
$output = '';
$options = shortcode_atts( array( 'capability' => '' ), $atts );
if ( $content !== null ) {
$groups_user = new Groups_User( get_current_user_id() );
$capability = $options['capability'];
$capabilities = array_map( 'trim', explode( ',', $capability ) );
$show_content = false;
foreach( $capabilities as $capability ) {
if ( $groups_user->can( $capability ) ) {
$show_content = true;
break;
}
}
if ( $show_content ) {
remove_shortcode( 'groups_can' );
$content = do_shortcode( $content );
add_shortcode( 'groups_can', array( __CLASS__, 'groups_can' ) );
$output = $content;
}
}
return $output;
}
Code file location:
groups/groups/lib/access/class-groups-access-shortcodes.php
Groups [groups_can_not] Shortcode
The ‘groups_can_not’ shortcode is a powerful tool in the Groups plugin. It restricts content visibility based on user capabilities. This shortcode checks if the current user possesses certain capabilities. If they do, the content within the shortcode remains hidden. If the user lacks the specified capabilities, the content becomes visible. This ensures a tailored user experience, enhancing the functionality of your WordPress site.
Shortcode: [groups_can_not]
Parameters
Here is a list of all possible groups_can_not shortcode parameters and attributes:
capability
– specifies the user’s abilities or roles
Examples and Usage
Basic Example – Displaying content to users who lack a specific capability
[groups_can_not capability="edit_posts"]You don't have permission to edit posts.[/groups_can_not]
In this example, the shortcode checks if the current user has the ‘edit_posts’ capability. If the user does not have this capability, the message “You don’t have permission to edit posts.” is displayed. This can be useful in informing users of their restrictions or guiding them to upgrade their membership for more capabilities.
Advanced Examples – Checking for multiple capabilities
[groups_can_not capability="edit_posts, publish_posts"]You don't have the required permissions.[/groups_can_not]
In this advanced example, the shortcode checks for multiple capabilities. It checks if the current user has either ‘edit_posts’ or ‘publish_posts’ capabilities. If the user lacks either of these capabilities, it displays the message “You don’t have the required permissions.” This can be used to inform users when they lack one or more necessary capabilities to perform a certain action.
PHP Function Code
In case you have difficulties debugging what causing issues with [groups_can_not]
shortcode, check below the related PHP functions code.
Shortcode line:
add_shortcode( 'groups_can_not', array( __CLASS__, 'groups_can_not' ) );
Shortcode PHP function:
function groups_can_not( $atts, $content = null ) {
$output = '';
$options = shortcode_atts( array( 'capability' => '' ), $atts );
if ( $content !== null ) {
$groups_user = new Groups_User( get_current_user_id() );
$capability = $options['capability'];
$capabilities = array_map( 'trim', explode( ',', $capability ) );
$show_content = true;
foreach( $capabilities as $capability ) {
if ( $groups_user->can( $capability ) ) {
$show_content = false;
break;
}
}
if ( $show_content ) {
remove_shortcode( 'groups_can_not' );
$content = do_shortcode( $content );
add_shortcode( 'groups_can_not', array( __CLASS__, 'groups_can_not' ) );
$output = $content;
}
}
return $output;
}
Code file location:
groups/groups/lib/access/class-groups-access-shortcodes.php
Groups [groups_login] Shortcode
The Groups Login shortcode is a useful tool in the Groups plugin for WordPress, allowing users to log in or out directly from any page. The shortcode utilizes the PHP function ‘groups_login’ to generate a login form. If ‘show_logout’ is set to ‘yes’, it provides a logout option for logged-in users. The ‘redirect’ attribute defines the page users are directed to post-login or logout.
Shortcode: [groups_login]
Parameters
Here is a list of all possible groups_login shortcode parameters and attributes:
redirect
– URL where users will be sent after loginshow_logout
– displays logout link if set to ‘yes’
Examples and Usage
Basic example – The shortcode ‘groups_login’ can be used to display a login form on your WordPress site. By default, it will redirect users to the current page after they log in.
[groups_login /]
Advanced examples
1. Customizing the redirect URL – You can specify a custom URL to redirect users to after they log in. Simply include the ‘redirect’ attribute in the shortcode and specify your desired URL.
[groups_login redirect="https://yourwebsite.com/yourpage" /]
2. Showing Logout Link – If you want to display a logout link to logged-in users, you can do so by setting the ‘show_logout’ attribute to ‘yes’. This can be useful if you want to provide users with a quick way to log out from the same page where they logged in.
[groups_login show_logout="yes" /]
3. Combining both parameters – You can also use both ‘redirect’ and ‘show_logout’ attributes together to customize the login form’s behavior further. Here’s an example:
[groups_login redirect="https://yourwebsite.com/yourpage" show_logout="yes" /]
PHP Function Code
In case you have difficulties debugging what causing issues with [groups_login]
shortcode, check below the related PHP functions code.
Shortcode line:
add_shortcode( 'groups_login', array( __CLASS__, 'groups_login' ) );
Shortcode PHP function:
function groups_login( $atts, $content = null ) {
$current_url = ( is_ssl() ? 'https://' : 'http://' ) . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
extract(
shortcode_atts(
array(
'redirect' => $current_url,
'show_logout' => 'no'
),
$atts
)
);
$redirect = trim( $redirect );
$show_logout = trim( strtolower( $show_logout ) );
$output = '';
if ( !is_user_logged_in() ) {
$output .= wp_login_form(
array(
'echo' => false,
'redirect' => $redirect
)
);
} else {
if ( $show_logout == 'yes' ) {
$output .= self::groups_logout(
array(
'redirect' => $redirect
)
);
}
}
return $output;
}
Code file location:
groups/groups/lib/views/class-groups-shortcodes.php
Groups [groups_logout] Shortcode
The Groups Logout shortcode enables users to log out from a WordPress site. It generates a logout URL, which redirects users to the current page after logging out.
Shortcode: [groups_logout]
Examples and Usage
Basic example – This shortcode allows the user to log out from the current page.
[groups_logout /]
PHP Function Code
In case you have difficulties debugging what causing issues with [groups_logout]
shortcode, check below the related PHP functions code.
Shortcode line:
add_shortcode( 'groups_logout', array( __CLASS__, 'groups_logout' ) );
Shortcode PHP function:
function groups_logout( $atts, $content = null ) {
$current_url = ( is_ssl() ? 'https://' : 'http://' ) . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
extract(
shortcode_atts(
array(
'redirect' => $current_url
),
$atts
)
);
$redirect = trim( $redirect );
$output = '';
if ( is_user_logged_in() ) {
$output .= sprintf( '<a href="%s">', esc_url( wp_logout_url( $redirect ) ) );
$output .= __( 'Log out', 'groups' );
$output .= '</a>';
}
return $output;
}
Code file location:
groups/groups/lib/views/class-groups-shortcodes.php
Groups [groups_group_info] Shortcode
The ‘Groups Group Info’ shortcode provides detailed information about a specific group. It fetches group data like name, description, count, and users. It allows customization through various attributes. You can specify the group by its name and choose what information to show. The ‘show’ attribute can display the group’s name, description, count of users, or the users themselves. The count is the number of users in the group, while ‘users’ lists all the users in the group. The output is neatly formatted and sanitized to ensure a clean display.
Shortcode: [groups_group_info]
Parameters
Here is a list of all possible groups_group_info shortcode parameters and attributes:
group
– Specifies the name or ID of the groupshow
– Determines the type of information to display: group name, description, user count, or usersformat
– Defines the output format, not used in this functionsingle
– Text to display when group has only one userplural
– Format of text to display for groups with multiple users, uses count placeholder
Examples and Usage
Basic example – Displaying the name of a group using the ‘groups_group_info’ shortcode.
[groups_group_info group="GroupName" show="name" /]
Advanced examples
Displaying the description of a group using the ‘groups_group_info’ shortcode.
[groups_group_info group="GroupName" show="description" /]
Displaying the count of users in a group using the ‘groups_group_info’ shortcode. The output will be singular or plural based on the count of users.
[groups_group_info group="GroupName" show="count" single="1 user" plural="%d users" /]
Displaying the list of users in a group using the ‘groups_group_info’ shortcode. The output will be a list of usernames.
[groups_group_info group="GroupName" show="users" /]
PHP Function Code
In case you have difficulties debugging what causing issues with [groups_group_info]
shortcode, check below the related PHP functions code.
Shortcode line:
add_shortcode( 'groups_group_info', array( __CLASS__, 'groups_group_info' ) );
Shortcode PHP function:
function groups_group_info( $atts, $content = null ) {
global $wpdb;
$output = "";
$options = shortcode_atts(
array(
'group' => '',
'show' => '',
'format' => '',
'single' => '1',
'plural' => '%d'
),
$atts
);
$group = trim( $options['group'] );
$current_group = Groups_Group::read( $group );
if ( !$current_group ) {
$current_group = Groups_Group::read_by_name( $group );
}
if ( $current_group ) {
switch( $options['show'] ) {
case 'name' :
$output .= wp_filter_nohtml_kses( $current_group->name );
break;
case 'description' :
$output .= wp_filter_nohtml_kses( $current_group->description );
break;
case 'count' :
$user_group_table = _groups_get_tablename( 'user_group' );
$count = $wpdb->get_var( $wpdb->prepare(
"SELECT COUNT(*) FROM $user_group_table WHERE group_id = %d",
Groups_Utility::id( $current_group->group_id )
) );
if ( $count === null ) {
$count = 0;
} else {
$count = intval( $count );
}
$output .= _n( $options['single'], sprintf( $options['plural'], $count ), $count, 'groups' );
break;
// @todo experimental - could use pagination, sorting, link to profile, ...
case 'users' :
$user_group_table = _groups_get_tablename( 'user_group' );
$users = $wpdb->get_results( $wpdb->prepare(
"SELECT * FROM $wpdb->users LEFT JOIN $user_group_table ON $wpdb->users.ID = $user_group_table.user_id WHERE $user_group_table.group_id = %d",
Groups_Utility::id( $current_group->group_id )
) );
if ( $users ) {
$output .= '<ul>';
foreach( $users as $user ) {
$output .= '<li>' . wp_filter_nohtml_kses( $user->user_login ) . '</li>';
}
$output .= '</ul>';
}
break;
}
}
return $output;
}
Code file location:
groups/groups/lib/views/class-groups-shortcodes.php
Groups [groups_user_groups] Shortcode
The Groups User Groups shortcode is designed to display the groups a user belongs to. It allows customization of output format and sorting. It accepts attributes like ‘user_id’, ‘user_login’, ‘user_email’ for user targeting. The ‘format’ attribute controls the output format, ‘list_class’ and ‘item_class’ define CSS classes for list and items respectively. The ‘order_by’ and ‘order’ attributes manage the sorting of groups. ‘group’ and ‘exclude_group’ attributes allow including or excluding specific groups.
Shortcode: [groups_user_groups]
Parameters
Here is a list of all possible groups_user_groups shortcode parameters and attributes:
user_id
– Identifies the user by their unique ID.user_login
– Specifies the user by their login name.user_email
– Defines the user by their email address.format
– Determines the format of the output, such as list or div.list_class
– Sets the CSS class for the list.item_class
– Assigns the CSS class for each list item.order_by
– Specifies sorting of the list by name or group ID.order
– Sets the order of the list, either ascending or descending.group
– Defines specific group(s) to be included in the list.exclude_group
– Specifies group(s) to be excluded from the list.
Examples and Usage
Basic example – Displaying the groups of the current user in a list format
[groups_user_groups /]
Advanced examples
Displaying the groups of a specific user by user ID and excluding a certain group. The groups are displayed in an ordered list format, sorted by name in descending order.
[groups_user_groups user_id="2" exclude_group="3" format="ol" order_by="name" order="DESC" /]
Displaying the groups of a specific user by user email in a unordered list format, sorted by group ID in ascending order. A specific class is assigned to the list and each item.
[groups_user_groups user_email="example@example.com" format="ul" list_class="my-list" item_class="my-item" order_by="group_id" /]
Displaying only specific groups of a user by user login. The groups are displayed in a div format.
[groups_user_groups user_login="username" group="1,2,3" format="div" /]
PHP Function Code
In case you have difficulties debugging what causing issues with [groups_user_groups]
shortcode, check below the related PHP functions code.
Shortcode line:
add_shortcode( 'groups_user_groups', array( __CLASS__, 'groups_user_groups' ) );
Shortcode PHP function:
function groups_user_groups( $atts, $content = null ) {
$output = '';
$options = shortcode_atts(
array(
'user_id' => null,
'user_login' => null,
'user_email' => null,
'format' => 'list',
'list_class' => 'groups',
'item_class' => 'name',
'order_by' => 'name',
'order' => 'ASC',
'group' => null,
'exclude_group' => null
),
$atts
);
$user_id = null;
if ( $options['user_id'] !== null ) {
if ( $user = get_user_by( 'id', $options['user_id'] ) ) {
$user_id = $user->ID;
}
} else if ( $options['user_id'] !== null ) {
if ( $user = get_user_by( 'login', $options['user_login'] ) ) {
$user_id = $user->ID;
}
} else if ( $options['user_email'] !== null ) {
if ( $user = get_user_by( 'email', $options['user_login'] ) ) {
$user_id = $user->ID;
}
}
if ( $user_id === null ) {
$user_id = get_current_user_id();
}
if ( $user_id !== null ) {
$user = new Groups_User( $user_id );
$groups = $user->groups;
if ( !empty( $groups ) ) {
// group attr
if ( $options['group'] !== null ) {
$groups = array();
$groups_incl = explode( ',', $options['group'] );
foreach ( $groups_incl as $group_incl ) {
$group = trim( $group_incl );
$current_group = Groups_Group::read( $group );
if ( !$current_group ) {
$current_group = Groups_Group::read_by_name( $group );
}
if ( $current_group ) {
if ( Groups_User_Group::read( $user_id, $current_group->group_id ) ) {
$groups[] = $current_group;
}
}
}
}
// exclude_group attr
if ( $options['exclude_group'] !== null ) {
$groups_excl = explode( ',', $options['exclude_group'] );
foreach ( $groups_excl as $key => $group_excl ) {
$group = trim( $group_excl );
$current_group = Groups_Group::read( $group );
if ( !$current_group ) {
$current_group = Groups_Group::read_by_name( $group );
}
if ( $current_group ) {
$groups_excl[$key] = $current_group->group_id;
} else {
unset( $groups_excl[$key] );
}
}
foreach ( $groups as $key => $group ) {
if ( in_array( $group->group_id, $groups_excl ) ) {
unset( $groups[$key] );
}
}
}
switch( $options['order_by'] ) {
case 'group_id' :
usort( $groups, array( __CLASS__, 'sort_id' ) );
break;
default :
usort( $groups, array( __CLASS__, 'sort_name' ) );
}
switch( $options['order'] ) {
case 'desc' :
case 'DESC' :
$groups = array_reverse( $groups );
break;
}
switch( $options['format'] ) {
case 'list' :
case 'ul' :
$output .= '<ul class="' . esc_attr( $options['list_class'] ) . '">';
break;
case 'ol' :
$output .= '<ol class="' . esc_attr( $options['list_class'] ) . '">';
break;
default :
$output .= '<div class="' . esc_attr( $options['list_class'] ) . '">';
}
foreach( $groups as $group ) {
switch( $options['format'] ) {
case 'list' :
case 'ul' :
case 'ol' :
$output .= '<li class="' . esc_attr( $options['item_class'] ) . '">' . $group->name . '</li>';
break;
default :
$output .= '<div class="' . esc_attr( $options['item_class'] ) . '">' . $group->name . '</div>';
}
}
switch( $options['format'] ) {
case 'list' :
case 'ul' :
$output .= '</ul>';
break;
case 'ol' :
$output .= '</ol>';
break;
default :
$output .= '</div>';
}
}
}
return $output;
}
Code file location:
groups/groups/lib/views/class-groups-shortcodes.php
Groups [groups_groups] Shortcode
The Groups Plugin shortcode is a tool that retrieves and displays groups from your database. It sorts these groups by ‘name’ or ‘group_id’, and displays them in ascending or descending order. This shortcode uses various formats (‘list’, ‘ul’, ‘ol’) to present the groups. It customizes the display with CSS classes for the list and each item, providing flexibility in styling.
Shortcode: [groups_groups]
Parameters
Here is a list of all possible groups_groups shortcode parameters and attributes:
format
– Determines the layout of the group list (list, ul, ol, or div).list_class
– Specifies the CSS class for the list container.item_class
– Specifies the CSS class for each list item.order_by
– Defines the group list order (by name or group_id).order
– Sets the direction of the order (ASC for ascending or DESC for descending).
Examples and Usage
Basic Example – Display a list of groups in ascending order by name.
[groups_groups format="list" list_class="groups" item_class="name" order_by="name" order="ASC"]
Advanced Examples
Display a list of groups in descending order by group ID, using ordered list format.
[groups_groups format="ol" list_class="groups" item_class="name" order_by="group_id" order="DESC"]
Display groups in a div format, ordered in ascending order by name.
[groups_groups format="div" list_class="groups" item_class="name" order_by="name" order="ASC"]
Display groups as an unordered list, ordered in ascending order by group ID.
[groups_groups format="ul" list_class="groups" item_class="name" order_by="group_id" order="ASC"]
PHP Function Code
In case you have difficulties debugging what causing issues with [groups_groups]
shortcode, check below the related PHP functions code.
Shortcode line:
add_shortcode( 'groups_groups', array( __CLASS__, 'groups_groups' ) );
Shortcode PHP function:
function groups_groups( $atts, $content = null ) {
global $wpdb;
$output = '';
$options = shortcode_atts(
array(
'format' => 'list',
'list_class' => 'groups',
'item_class' => 'name',
'order_by' => 'name',
'order' => 'ASC'
),
$atts
);
switch( $options['order_by'] ) {
case 'group_id' :
case 'name' :
$order_by = $options['order_by'];
break;
default :
$order_by = 'name';
}
switch( $options['order'] ) {
case 'asc' :
case 'ASC' :
case 'desc' :
case 'DESC' :
$order = strtoupper( $options['order'] );
break;
default :
$order = 'ASC';
}
$group_table = _groups_get_tablename( 'group' );
if ( $groups = $wpdb->get_results(
"SELECT group_id FROM $group_table ORDER BY $order_by $order"
) ) {
switch( $options['format'] ) {
case 'list' :
case 'ul' :
$output .= '<ul class="' . esc_attr( $options['list_class'] ) . '">';
break;
case 'ol' :
$output .= '<ol class="' . esc_attr( $options['list_class'] ) . '">';
break;
default :
$output .= '<div class="' . esc_attr( $options['list_class'] ) . '">';
}
foreach( $groups as $group ) {
$group = new Groups_Group( $group->group_id );
switch( $options['format'] ) {
case 'list' :
case 'ul' :
case 'ol' :
$output .= '<li class="' . esc_attr( $options['item_class'] ) . '">' . $group->name . '</li>';
break;
default :
$output .= '<div class="' . esc_attr( $options['item_class'] ) . '">' . $group->name . '</div>';
}
}
switch( $options['format'] ) {
case 'list' :
case 'ul' :
$output .= '</ul>';
break;
case 'ol' :
$output .= '</ol>';
break;
default :
$output .= '</div>';
}
}
return $output;
}
Code file location:
groups/groups/lib/views/class-groups-shortcodes.php
Groups [groups_join] Shortcode
The ‘groups_join’ shortcode from the Groups plugin allows users to join a specific group. It uses the ‘groups_join’ function to display a join form or a message if the user is already a member.
Shortcode: [groups_join]
Parameters
Here is a list of all possible groups_join shortcode parameters and attributes:
group
– identifies the group a user wishes to joindisplay_message
– controls if a message is displayed after joining a groupdisplay_is_member
– shows a message if the user is already a member of the groupsubmit_text
– customizes the text displayed on the join button
Examples and Usage
Basic example – A simple shortcode for a user to join a group. The group name is specified as an attribute in the shortcode.
[groups_join group="GroupName" /]
Advanced examples
Using the shortcode with an option to display a message after a user joins a group. The message will be displayed if ‘display_message’ attribute is set to true.
[groups_join group="GroupName" display_message=true /]
Using the shortcode with an option to display a custom submit button text. The text will replace the default ‘Join the %s group’ text.
[groups_join group="GroupName" submit_text="Click here to join the %s group" /]
Using the shortcode with multiple attributes. This example includes a group name to join, a custom submit button text, and an option to display a message after a user joins a group.
[groups_join group="GroupName" display_message=true submit_text="Click here to join the %s group" /]
Using the shortcode with an option to display a message if a user is already a member of the group. The message will be displayed if ‘display_is_member’ attribute is set to true.
[groups_join group="GroupName" display_is_member=true /]
PHP Function Code
In case you have difficulties debugging what causing issues with [groups_join]
shortcode, check below the related PHP functions code.
Shortcode line:
add_shortcode( 'groups_join', array( __CLASS__, 'groups_join' ) );
Shortcode PHP function:
function groups_join( $atts, $content = null ) {
$nonce_action = 'groups_action';
$nonce = 'nonce_join';
$output = '';
$options = shortcode_atts(
array(
'group' => '',
'display_message' => true,
'display_is_member' => false,
'submit_text' => __( 'Join the %s group', 'groups' )
),
$atts
);
extract( $options );
if ( $display_message === 'false' ) {
$display_message = false;
}
if ( $display_is_member === 'true' ) {
$display_is_member = true;
}
$group = trim( $options['group'] );
$current_group = Groups_Group::read( $group );
if ( !$current_group ) {
$current_group = Groups_Group::read_by_name( $group );
}
if ( $current_group ) {
if ( $user_id = get_current_user_id() ) {
$submitted = false;
$invalid_nonce = false;
if ( !empty( $_POST['groups_action'] ) && $_POST['groups_action'] == 'join' ) {
$submitted = true;
if ( !wp_verify_nonce( $_POST[$nonce], $nonce_action ) ) {
$invalid_nonce = true;
}
}
if ( $submitted && !$invalid_nonce ) {
// add user to group
if ( isset( $_POST['group_id'] ) ) {
$join_group = Groups_Group::read( $_POST['group_id'] );
Groups_User_Group::create(
array(
'group_id' => $join_group->group_id,
'user_id' => $user_id
)
);
}
}
if ( !Groups_User_Group::read( $user_id, $current_group->group_id ) ) {
$submit_text = sprintf( $options['submit_text'], wp_filter_nohtml_kses( $current_group->name ) );
$output .= '<div class="groups-join">';
$output .= '<form action="#" method="post">';
$output .= '<input type="hidden" name="groups_action" value="join" />';
$output .= '<input type="hidden" name="group_id" value="' . esc_attr( $current_group->group_id ) . '" />';
$output .= '<input type="submit" value="' . $submit_text . '" />';
$output .= wp_nonce_field( $nonce_action, $nonce, true, false );
$output .= '</form>';
$output .= '</div>';
} else if ( $display_message ) {
if ( $submitted && !$invalid_nonce && isset( $join_group ) && $join_group->group_id === $current_group->group_id ) {
$output .= '<div class="groups-join joined">';
$output .= sprintf( __( 'You have joined the %s group.', 'groups' ), wp_filter_nohtml_kses( $join_group->name ) );
$output .= '</div>';
}
else if ( $display_is_member && isset( $current_group ) && $current_group !== false ) {
$output .= '<div class="groups-join member">';
$output .= sprintf( __( 'You are a member of the %s group.', 'groups' ), wp_filter_nohtml_kses( $current_group->name ) );
$output .= '</div>';
}
}
}
}
return $output;
}
Code file location:
groups/groups/lib/views/class-groups-shortcodes.php
Groups [groups_leave] Shortcode
The Groups_Leave shortcode is a functional tool in WordPress that allows users to leave a specific group. It’s part of the Groups plugin. The shortcode works by verifying the current user and the group they intend to leave. If the user is indeed part of the group and the nonce verification passes, the user is successfully removed from the group. The shortcode also provides an option to display a message upon successful departure from the group.
Shortcode: [groups_leave]
Parameters
Here is a list of all possible groups_leave shortcode parameters and attributes:
group
– specifies the name of the group to leavedisplay_message
– controls if a message is shown after leaving the groupsubmit_text
– sets the text displayed on the “leave group” button
Examples and Usage
Basic example – This shortcode allows a user to leave a specific group. The group is identified by its name.
[groups_leave group="Group Name" /]
PHP Function Code
In case you have difficulties debugging what causing issues with [groups_leave]
shortcode, check below the related PHP functions code.
Shortcode line:
add_shortcode( 'groups_leave', array( __CLASS__, 'groups_leave' ) );
Shortcode PHP function:
function groups_leave( $atts, $content = null ) {
$nonce_action = 'groups_action';
$nonce = 'nonce_leave';
$output = '';
$options = shortcode_atts(
array(
'group' => '',
'display_message' => true,
'submit_text' => __( 'Leave the %s group', 'groups' ),
),
$atts
);
extract( $options );
if ( $display_message === 'false' ) {
$display_message = false;
}
$group = trim( $options['group'] );
$current_group = Groups_Group::read( $group );
if ( !$current_group ) {
$current_group = Groups_Group::read_by_name( $group );
}
if ( $current_group ) {
if ( $user_id = get_current_user_id() ) {
$submitted = false;
$invalid_nonce = false;
if ( !empty( $_POST['groups_action'] ) && $_POST['groups_action'] == 'leave' ) {
$submitted = true;
if ( !wp_verify_nonce( $_POST[$nonce], $nonce_action ) ) {
$invalid_nonce = true;
}
}
if ( $submitted && !$invalid_nonce ) {
// remove user from group
if ( isset( $_POST['group_id'] ) ) {
$leave_group = Groups_Group::read( $_POST['group_id'] );
Groups_User_Group::delete( $user_id, $leave_group->group_id );
}
}
if ( Groups_User_Group::read( $user_id, $current_group->group_id ) ) {
$submit_text = sprintf( $options['submit_text'], wp_filter_nohtml_kses( $current_group->name ) );
$output .= '<div class="groups-join">';
$output .= '<form action="#" method="post">';
$output .= '<input type="hidden" name="groups_action" value="leave" />';
$output .= '<input type="hidden" name="group_id" value="' . esc_attr( $current_group->group_id ) . '" />';
$output .= '<input type="submit" value="' . $submit_text . '" />';
$output .= wp_nonce_field( $nonce_action, $nonce, true, false );
$output .= '</form>';
$output .= '</div>';
} else if ( $display_message ) {
if ( $submitted && !$invalid_nonce && isset( $leave_group ) && $leave_group->group_id === $current_group->group_id ) {
$output .= '<div class="groups-join left">';
$output .= sprintf( __( 'You have left the %s group.', 'groups' ), wp_filter_nohtml_kses( $leave_group->name ) );
$output .= '</div>';
}
}
}
}
return $output;
}
Code file location:
groups/groups/lib/views/class-groups-shortcodes.php
Conclusion
Now that you’ve learned how to embed the 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.
Leave a Reply