Tagged: x
-
AuthorPosts
-
January 5, 2017 at 3:20 am #1317071
Hi,
I found this thread from 2 years ago and for me it’s really useful.
I have two questions: is the script for functions.php correct? I can’t get it to work.
And second: is it possible to tweak the code with an additional class for content that is only visible to logged out users?Thanks!
Here is the code:
in functions.php
// Add specific CSS class by filter // Used to hide content based on user role 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; }
in css
/* content hide/show based on user role */ .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; }
shortcodes
[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]
January 5, 2017 at 6:35 am #1317225Hi,
Thanks for writing!
Yes, the code looks correct. Can you provide us your site url so we can take a closer look.
Then add this in custom css
.admin-bar .logged-out-users-only { display:none !important; } .logged-out-users-only { display:block; }
After that you can use this shortcode
[content_band class="logged-out-users-only"] can be viewed by logged out users only[/content_band]
Hope that helps.
January 6, 2017 at 3:01 am #1318566Hi, thanks for your help! The additional CSS works great.
The url of the website is http://wp.sanopharm.com.
I added two menu items: “logged in” and “logged out” (I named it easily for convenient reasons). “Logged in” has the class
for-logged-in-users
and “logged out” has the classfor-logged-out-users
. That should result in hiding the menu item “logged in” for users who are logged out, and “logged-out” should be visible for all users, right?Oh and I believe a noticed a small error in the CSS code. Is the second
.for-logged-
out
-users
correct or should it be.for-logged-
in
-uses
?.logged-in-{role} .for-logged-out-users { display: none !important; } .logged-in-{role} .for-logged-out-users { display: block !important; }
January 6, 2017 at 4:10 am #1318639Hi,
Instead of the codes above please use this plugin instead.
https://wordpress.org/plugins/baw-login-logout-menu/
Thanks
January 6, 2017 at 8:07 am #1318811Hi,
Thanks but it’s only part of the problem. This page (http://wp.sanopharm.com/voorbeeld-pagina/) contains the following code but it’s not working as expected.
[content_band class="for-logged-out-users"] can be viewed by anyone [/content_band] [content_band class="for-logged-in-admin"] Only for LOGGED IN users. [/content_band] [content_band class="logged-out-users-only"] can be viewed by logged out users only[/content_band] [content_band class="logged-in-users-only"] can be viewed by logged in users only[/content_band]
January 6, 2017 at 12:05 pm #1319106Hi again,
Would you mind providing us with login credentials so we can take a closer look? To do this, you can make a post with the following info:
– Link to your site
– WordPress Admin username / passwordDon’t forget to select Set as private reply. This ensures your information is only visible to our staff.
January 8, 2017 at 9:55 am #1321143This reply has been marked as private.January 8, 2017 at 6:28 pm #1321556Hi there,
I’m trying to understand it but it’s quite confusing. Example, you wish to display it for the user of a specific role, then also display it when the user is logged in.
A user with a role is also logged in, this means there is no point of displaying it based on the role as it’s already displayed since the user needs to be logged in. It’s impossible to get user role without logging in.
And this .logged-in-{role} is a wildcard, it should be like .logged-in-administrator, .logged-in-author, and etc. It will not work just by .logged-in-{role}.
The problem that I could see is the limitation of CSS, especially when creating a combination of conditions. It’s more effective on server side coding (PHP) since ROLE is stored in the database.
Example, something like this
[access_restriction role="administrator"] I'm the administrator! [/access_restriction] [access_restriction role="author"] I'm the author! [/access_restriction] [access_restriction role="any"] I can be of any role, just as long as I'm logged in [/access_restriction] [access_restriction role="none"] I can be accessed by non-logged in users [/access_restriction] And I don't have any restriction, I can be access both by logged in or out users.
The PHP code that you could add to your child theme’s functions.php is this
add_shortcode('access_restriction', 'access_restriction_function'); function access_restriction_function ( $atts, $content ) { $role = empty( $atts['role'] ) ? 'any' : $atts['role']; switch ( $role ) { case 'any': return is_user_logged_in() ? do_shortcode( $content ) : ''; break; case 'none': return is_user_logged_in() ? '' : do_shortcode( $content ); break; default : if ( !is_user_logged_in() ) return ''; $roles = wp_get_current_user()->roles; return $role == $roles || in_array( $role, $roles ) ? do_shortcode( $content ) : ''; break; } }
Then you can simply add your content as the sample above.
Hope this helps.
January 19, 2017 at 9:51 am #1336776Hi Rad,
First of all, very very sorry for the delay! It was very busy with work. And sorry for the confusion. The good thing is that you understood exactly what I wanted!
Thanks for the shortcodes, they work like a charm!
One extra question: is it possible to use this method to show/hide menu items based on the role of a user?
January 19, 2017 at 11:42 am #1336942Hey there,
Thanks for writing in! Regretfully, this particular customization request is outside the scope of our support as this is not related to an issue with the theme and instead has to do with your customization of it. As such, you will need to investigate this particular issue on your own or seek help from a developer should you not feel comfortable making these changes yourself. If you have any further questions about the theme, we are more than happy to provide you with assistance on these inquiries.
Thank you for your understanding.
January 24, 2017 at 1:37 pm #1343552I understand. Thanks anyway for your help, it has ben really helpful!
January 24, 2017 at 4:02 pm #1343782You’re most welcome. Thank you so much for understanding.
Feel free to ask us again.
Thanks.
January 25, 2017 at 6:50 am #1344614Hey!
I got another question about the shortcode. How do I assign multiple roles to a shortcode? The code below doesn’t work. Thanks!
[access_restriction role="author, subscriber"] I'm the author! [/access_restriction]
January 25, 2017 at 11:05 am #1344965Hey there,
It could be possible with custom development, but this would be outside the scope of support we can offer. You may wish to consult a developer to assist you with this. X is quite extensible with child themes, so there are plenty of possibilities.
Thanks for understanding. Take care!
-
AuthorPosts