Are you a GenerateCustomer?

Do you have an active GP Premium or GenerateBlocks Pro license key?

Create a GenerateSupport account for GeneratePress and GenerateBlocks support in one dedicated place.

Create an account
Already have a GenerateSupport account? Login

Just browsing?

Feel free to browse the forums. Support for our free versions is provided on (GeneratePress, GenerateBlocks).

Want to become a premium user? Learn more below.

Hide container based on user role?

  • blackbirdsolutions

    Hi, I would appreciate some guidance concerning how to show/hide content to guests versus logged in members.
    I use plugin “User role editor”. We have member A and B.
    I know how to hide pages, posts and so on, but is it possible to hide a Generatpress container or specificly only one of many Grid containers from a specific user role?

    I know I can restrict part of a content by inserting User role editor “shortcode”. But that code sometimes creates a gap in the layout.

    And as fas as I can see I can not shortcode around one specific inline container in a grid? Can I maybe choose what user role can see the container in another way??

    For example six containers in a grid with container links to six different pages. But one of the pages you must be logged in as user role A to have access to. So I would like to hide that container IF user is no-role or B. And show another container to them with just a text “This is for members A only” and no link.

  • Hi there,

    it can be done with some code, but there are plugins like this one:

  • blackbirdsolutions

    Aha, well I do not want more plugins. (For now I duplicated the grid with all containers/items and made two alternatives, and with plugin User role shortcodes I show one grid to user role A and the other to user role B.)

    I was more hoping that maybe something under Block settings > Advanced > “Custom attributes” could do the trick?

    I can see under “allowed attributes” that “roles” is mentioned. But I have no idea if that can be used to say “show this block only to role A and B”, and if so: How to do that?? (I never used Attributes)

    The following HTML attributes are allowed to be used in the Custom Attributes feature:


  • Custom Attributes are for adding attributes to the blocks HTML such as an aria-label or a role="list" – they aren’t for controlling a blocks behaviour.

    I would opt for the plugin personally as its a lot more convenient and there code will be more robust then what i can offer here.

    If you really want to use code then you can add this PHP Snippet to your site:

    add_filter( 'render_block', function( $block_content, $block ) {
      if ( 
          !is_admin() && ! empty( $block['attrs']['className'] ) && 
          strpos( $block['attrs']['className'], 'has-user-role' ) !== false ) 
        $user = wp_get_current_user();
        $disallowed_roles = array( 'editor', 'author' );
        if ( array_intersect( $disallowed_roles, $user->roles ) ) {
           return '';
      return $block_content;
    }, 10, 2 );

    In the code is $disallowed_roles = array( 'editor', 'author' ); where you specify the roles for which the block should be removed.

    Then give the block a CSS class of: has-user-role

  • blackbirdsolutions

    Thank you for the optional code and the explanation of attributes.

    Well, then I stick to my shortcode for now and probably get back to it later in life. Things tend to change and evolve in the wordpress world every year…

  • For sure 🙂 Glad to be of help

Viewing 6 posts - 1 through 6 (of 6 total)
  • You must be logged in to reply to this topic.