Your cart is currently empty!
Shortcode groups_member
โ
by
Hi there.
It would be great if you included the shortcode below in the next update. The group_member shortcode unfortunately only makes an “or” comparison and returns true if the user is in one of the groups listed. The attached member_in_all_groups shortcode only outputs the content if the user is in all groups in the list.
Best regards
Guido
/**
* Takes one attribute “group” which is a comma-separated list of group
* names or ids (can be mixed).
* The content is shown if the current user belongs to all groups in list.
*
* @param array $atts attributes
* @param string $content content to render
*/
public static function member_in_all_groups( $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 = 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( ‘member_in_all_groups’ );
$content = do_shortcode( $content );
add_shortcode( ‘member_in_all_groups’, array( __CLASS__, ‘member_in_all_groups’ ) );
$output = $content;
}
}
return $output;
}
Comments
3 responses to “Shortcode groups_member”
Hey Guido,
You’ll know it next time. We keep many repositories with extras which are public and the most interesting of them are also listed in our documentation, but all you can do is ask. ๐
Kind regards,
George
Thank you George.
If I had known the code, I could have saved myself the work. Why are you hiding it so well ๐
Hi Guido,
Thanks for mentioning your approach. For this use case we also provide the groups-cmember shortode which is the conjunctive version of the shortcode.
Feel free to use the version you like.
Kind regards,
George