Shortcode groups_member

Posted in

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;
}

3 Responses to Shortcode groups_member

  1. George January 21, 2021 at 1:49 pm #

    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

  2. GMT-IT January 20, 2021 at 2:46 pm #

    Thank you George.

    If I had known the code, I could have saved myself the work. Why are you hiding it so well 馃槈

  3. George January 20, 2021 at 1:56 pm #

    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

We use cookies to optimize your experience on our site and assume you're OK with that if you stay.
OK, hide this message.

AffiliatesContactJobsTerms & ConditionsPrivacy PolicyDocumentationDownloadsUseful PluginsMy Account

Share