Jump to content

How do I show a DIV, only to certain user roles in Wordpress?


simona6
 Share

Recommended Posts

We have a search box in our website, but we want it to appear only when someone is logged in, and 'subscribed', as it reveals the shop.  At the moment, they cannot access the shop, but the search would reveal shop entries.

So two questions:

1) how can I assign a class based on User Role, that display: none to a search DIV within the theme?
2) "uk/?s=fred&post_type=product".  How can I hide or 'redirect' this type of URL to another, based on user role?  I can redirect pages, but this is not really "a page"... is it?

Thanks

 

Simon

Link to comment
Share on other sites

The answer to both of your questions is is_user_logged_in(). For the first situation, check it in your template before outputting the div or class, and in the second situation check the value using the template_rediect action.

Oh, and "uk/?s=fred&post_type=product" is a page (post, in WP's case) - it's '/uk/' with URL parameters 's' and 'product'.

Edited by maxxd
Link to comment
Share on other sites

Sorry the UK bit is the end of the domain.. .co.uk/?s=......... and so on.  It's not a page as such.

So how would I write a function that checks if they are logged in, and checks from the array of user roles. 

If not logged in, they we hide the DIV.  But if they are logged in, but their user role is NOT one of those specified we hide the DIV.

Link to comment
Share on other sites

https://stackoverflow.com/questions/50907170/wordpress-if-user-role-css-class-then-display-none

This seems to partially answer the question, but it doesn't state how to tell which roles will HIDE the DIV... only if it is 'logged in'.

If someone is logged in they can view the Wall, but we need it hidden for customer and subscriber.

Link to comment
Share on other sites

23 hours ago, simona6 said:

Sorry the UK bit is the end of the domain.. .co.uk/?s=......... and so on.  It's not a page as such.

So how would I write a function that checks if they are logged in, and checks from the array of user roles. 

If not logged in, they we hide the DIV.  But if they are logged in, but their user role is NOT one of those specified we hide the DIV.

The way WordPress (and most of the internet, nowadays) works is that it uses a pattern called a front controller. Every request is routed through the 'index.php' file, so https://mydomain.uk is a page, and everything after the question mark is a URL variable. For instance, 's' is specifically what WordPress looks for to know it's being asked to search for something. Click the links in my earlier post and you'll have your answer - use is_logged_in in the template file and the template_redirect action hook.

If you don't know how to do that, start here.

Link to comment
Share on other sites

don't worry, our Access Control plugin actually does it!  Oddly enough.
But... is there a way to apply display:none to a CSS class... based on user role.  Seen loads about this.  But none that are in that great detail.

If we can't, then the access control is fine.  but be good to hide the div!

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.