[resolved] Restricting Content in WordPress Templates (8 posts)

  1. learnthat
    Posted 1 year ago #

    Is there a way to restrict content to members of a group in a theme template file? And display content to people who aren't members of a group?

    In other words, is there a PHP equivalent to the groups_member group and groups_non_member shortcodes that I can use in a template file?



  2. Yep!:

    if( is_user_logged_is() ) {
  3. itthinx
    Plugin Author

    Posted 1 year ago #

    Using the is_user_logged_in() function is fine if the content is to be shown to a user who is logged in only, but @learnthat is asking if one can check based on group membership. In the Examples section it is shown how to check for group membership, that method can be used in a template to show content conditionally.

  4. joshseltzer
    Posted 1 year ago #

    So, I totally tried what you have based on the 'examples' page, but I must be doing something dumb, just not working for me. Is the way I have this code right?

      $is_a_member = false;
      require_once( ABSPATH . 'wp-includes/pluggable.php' );
      if ( $group = Groups_Group::read_by_name( 'Groupname' ) ) {
        $is_a_member = Groups_User_Group::read( get_current_user_id() , $group->group_id );
        // Do Stuff Here like maybe php html or jazz hands
  5. WPSeeker
    Posted 1 year ago #

    itthinx, I had the same problem as joshseltzer. Your example code doesn't work as intended.

    What I found that it does do is that it checks if that group name exists, but not if the user belongs to it. Can you please supply a code example that does work.


  6. itthinx
    Plugin Author

    Posted 1 year ago #

    @joshseltzer @WPSeeker the code is correct but you're missing one obvious thing here, the conditional enclosing what you want to happen if it's a member:

    if ( $is_a_member ) {
        // do what needs to be done for the member, for example ...
        echo 'Foo!';
  7. WPSeeker
    Posted 1 year ago #

    OK that worked! Thanks for the reply

  8. Marinko
    Posted 1 year ago #


    I need to hide WooCommerce stock info from public and make it available only to a group of users.
    It would be nice to have complete code that goes to theme function file.

    Thank you.

Topic Closed

This topic has been closed to new replies.

About this Plugin

  • Groups
  • Frequently Asked Questions
  • Support Threads
  • Reviews

About this Topic