simona6 Posted February 11, 2021 Share Posted February 11, 2021 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 Quote Link to comment https://forums.phpfreaks.com/topic/312130-how-do-i-show-a-div-only-to-certain-user-roles-in-wordpress/ Share on other sites More sharing options...
maxxd Posted February 11, 2021 Share Posted February 11, 2021 (edited) 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 February 11, 2021 by maxxd Quote Link to comment https://forums.phpfreaks.com/topic/312130-how-do-i-show-a-div-only-to-certain-user-roles-in-wordpress/#findComment-1584379 Share on other sites More sharing options...
simona6 Posted February 11, 2021 Author Share Posted February 11, 2021 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. Quote Link to comment https://forums.phpfreaks.com/topic/312130-how-do-i-show-a-div-only-to-certain-user-roles-in-wordpress/#findComment-1584381 Share on other sites More sharing options...
simona6 Posted February 12, 2021 Author Share Posted February 12, 2021 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. Quote Link to comment https://forums.phpfreaks.com/topic/312130-how-do-i-show-a-div-only-to-certain-user-roles-in-wordpress/#findComment-1584408 Share on other sites More sharing options...
maxxd Posted February 12, 2021 Share Posted February 12, 2021 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. Quote Link to comment https://forums.phpfreaks.com/topic/312130-how-do-i-show-a-div-only-to-certain-user-roles-in-wordpress/#findComment-1584411 Share on other sites More sharing options...
simona6 Posted February 12, 2021 Author Share Posted February 12, 2021 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! Quote Link to comment https://forums.phpfreaks.com/topic/312130-how-do-i-show-a-div-only-to-certain-user-roles-in-wordpress/#findComment-1584412 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.