Navigation
This is archived content. Visit our new forum.
  • Author
    Posts
  • #218684
    harleyltd
    Participant

    Is it possible to hide a row or column based on a user’s role? I can see that it is possible to use a class to hide a row but I am not savvy enough to know how to create a class to the functions.php file identifying a user role. http://codex.wordpress.org/Function_Reference/body_class#Add_Classes_By_Filters

    If the user role is Subscriber or a unique role I added such as Hero, would you be willing to show me how to tweak this code to make it work?

    // Add specific CSS class by filter
    add_filter( 'body_class', 'my_class_names' );
    function my_class_names( $classes ) {
    	// add 'class-name' to the $classes array
    	$classes[] = 'class-name';
    	// return the $classes array
    	return $classes;
    }

    Then I can add that class to the child css file and using it in the rows. PLEASE and thank you!

    #218827
    harleyltd
    Participant

    I had previously asked, via Themeforest comments, if it was possible to hide a row or column based on logged in/logged out. The response given did not work. It hid the whole age and not just the row. Here was the suggestion provided.

    WordPress adds a class to the <body> element when a user is logged in that is (surprisingly enough) called logged-in. If you added a custom class to a Content Band such as:

    [content_band class="my-class"] ... [/content_band]

    Then you could implement some CSS in your child theme like so to hide the band when logged in:

    .logged-in .my-class {
      display: none !important;
    }

    I added the CSS and then added the class to the Content Band settings but it hid the whole page instead of just the row. What did I do wrong?

    #218897
    Rue Nel
    Moderator

    Hi There,

    To add a custom class for logged in user, you can use this custom filter in your functions.php

    // Add specific CSS class by filter
    add_filter( 'body_class', 'logged_user_roles' );
    function logged_user_roles( $classes ) {
      if ( is_user_logged_in() ) {
        $current_user = wp_get_current_user();
        // add 'logged-in-roles' to the $classes array
        $classes[] = 'logged-in-' . implode(' logged-in-', $current_user->roles);
      }
    
      // return the $classes array
      return $classes;
    }

    Make sure that you enclose your content for that user roles in a separate content band.

    [content_band] can be viewed by anyone [/content_band]
    [content_band class="custom-class"] Only for subscribers [/content_band]

    And in your css, you use this.

    .logged-in-subscriber .custom-class {
      display: none !important;
    }

    Please let us know if this works for you.

    #218921
    harleyltd
    Participant

    That worked great. Thank you!!!

    #218937
    harleyltd
    Participant

    Just realized that only fixed half the problem. I want to show one row/content band if the user is logged in and a different one if the visitor is logged out which means I need to create a version to display: none if they are logged out? I hate to be a bother but can you share the code to make this happen? This may be a cool option to go ahead and include in future releases. THANK YOU for all of your help.

    #218957
    Rue Nel
    Moderator

    Hi There,

    Using our previous codes above, we could have something like this,

    [content_band class="logged-out"] can be viewed by anyone [/content_band]
    [content_band class="logged-in"] Only for LOGGED IN subscribers [/content_band]

    And in your css, you use this,

    
    .logged-out {
      display:block !important;
    }
    
    .logged-in {
      display: none !important;
    }
    
    .logged-in-subscriber .logged-out {
      display: none !important;
    }
    
    .logged-in-subscriber .logged-in {
      display: block !important;
    }

    Please let us know if this works.

    #219395
    harleyltd
    Participant

    If I only wanted to show a block to a logged in user, wouldn’t it be the opposite? logged-out = display:none and logged-in = display:block? If that is true, would the second half have stay the same or would it have to be reversed as well?

    #219643
    Rue Nel
    Moderator

    Hi There,

    The first two set are the general setting which means that the public view of the boxes. The second set overrides the first set because the user has logged in.

    Hope this make sense.

    Thank you.

    #219765
    harleyltd
    Participant

    Unfortunately, it doesn’t work. When I add that snippet to the custom css section under customize, the whole page disappears. Any other ideas? I’d love for this to work

    #219774
    Rue Nel
    Moderator

    Hi There,

    Would you mind providing us the url of your site with login credentials, if necessary, so we can take a closer look? We need to check your custom functions and css. To do this, you can make a post with the following info:

    – Link to your site

    – WordPress Admin username / password (only if necessary)

    Don’t forget to select Set as private reply. This ensures your information is only visible to our staff.

    Thank you.

    #219793
    harleyltd
    Participant
    This reply has been marked as private.
    #219831
    Rue Nel
    Moderator

    Hi There,

    Sorry about the confusion. We used the custom class as an example. You are meant to change that to your own name. We will start again here:

    [content_band class="for-logged-out-users"] can be viewed by anyone [/content_band]
    [content_band class="for-logged-in-users"] Only for LOGGED IN {user role} [/content_band]

    and in our custom css, we would have this:

    .for-logged-out-users {
      display:block !important;
    }
    
    .for-logged-in-users {
      display: none !important;
    }
    
    .logged-in-{role} .for-logged-out-users {
      display: none !important;
    }
    
    .logged-in-{role} .for-logged-out-users {
      display: block !important;
    }

    We hope it makes sense this time around 🙂

    #220261
    harleyltd
    Participant

    WORKS PERFECTLY!!! Thank you

    #220581
    Rue Nel
    Moderator

    Awesome! We’re glad it perfectly works for you 😉

    #229325
    harleyltd
    Participant

    Can you elaborate on one item in the code you shared above? Listed here below. The last two sections have the same class but different display. Is that correct? Or was it typed wrong? For example should one of them be ‘for-logged-in-users’ and one ‘for-logged-out-users’?

    .for-logged-out-users {
      display:block !important;
    }
    
    .for-logged-in-users {
      display: none !important;
    }
    
    .logged-in-{role} .for-logged-out-users {
      display: none !important;
    }
    
    .logged-in-{role} .for-logged-out-users {
      display: block !important;
    }

    And then if I wanted on one user-role type to see the content, I would use the class=logged-in-administrator

  • <script> jQuery(function($){ $("#no-reply-218684 .bbp-template-notice, .bbp-no-topic .bbp-template-notice").removeClass('bbp-template-notice'); }); </script>