Peter Knight Posted September 15, 2015 Share Posted September 15, 2015 Hi guys I'm attempting to do something new (to me) in PW and a search is coming up blank. I suspect I'm using the wrong search terms. I'd like to create a simple content block that is only shown to distinct PW users. The overall page itself would be public and viewable by anyone but there's a particular DIV which only shows to PW user X or Y. Can someone point me in the right direction? I thought this would be session based but having searched the forums and Google I'm coming up with results on hiding back-end content etc instead of front in within public pages. Cheers P Link to comment Share on other sites More sharing options...
apeisa Posted September 15, 2015 Share Posted September 15, 2015 I would use permissions or take a shurtcut and use roles for that. The shortest route is to use user, but I think it is messy even in your situation, since you have at least two users that can see the content ("user X or Y"). - So first create (and publish) new permission, lets call it "view-hidden" - Create new role (or use existing role) and add the new permission to all roles that should be able to view hidden content - Now make sure that user X and Y have roles that have "view-hidden" permission Then on your template code: if ($user->hasPermission("view-hidden")) { echo "This is hidden content block"; } If you took the shortcut with using roles directly, then it would be: if ($user->hasRole("admin")) { echo "This is hidden content block"; } 2 Link to comment Share on other sites More sharing options...
Peter Knight Posted September 15, 2015 Author Share Posted September 15, 2015 Thanks apesia. So simple! Now that I know where to look, I notice that here is a $user->name property. Can't I do this? if ($user->name("Peter")) { echo "Hidden content"; } or refer to a user by pageID if ($user->id("41")) { echo "Hidden content"; } I'm not at my installation right now. Link to comment Share on other sites More sharing options...
apeisa Posted September 15, 2015 Share Posted September 15, 2015 You could check by name or id (or any custom field also), but those are properties, not methods. So correct syntax would be: if ($user->name == "Peter") { echo "Hidden content"; } // or if ($user->id == 41) { echo "Hidden content"; } But as I said earlier, checking only by user is a shortcut I wouldn't take even on smallest sites: There is nothing on the admin site to tell the difference that some users are handled differently If you need to add or remove this hidden content from some user, it is only possible through code So I strongly suggest using permission or role for this purpose. Link to comment Share on other sites More sharing options...
Peter Knight Posted September 15, 2015 Author Share Posted September 15, 2015 Thanks. I completely take your advice. At the moment I'm just showing some content for myself while I develop a new part of a page so this will work great. 1 Link to comment Share on other sites More sharing options...
Tom. Posted September 19, 2015 Share Posted September 19, 2015 I was wondering what you use the $permissions API for?I was trying to do if($permissions->get("page-edit")) { // content here } however it was $user->hasPermission("page-edit") I soon figured out. Link to comment Share on other sites More sharing options...
Martijn Geerts Posted September 19, 2015 Share Posted September 19, 2015 I was wondering what you use the $permissions API for? You can build logic upon a permission. if ($user->hasPermission('just-a-permission')) { // do something } Every user has role. (A Role extend Page, so behaves like a page)Every role can have multiple permissions (A Permission extends Page, so behaves like a page) Every user can have multiple roles. When you ask $user->hasPermission('something'), ProcessWire will loop all roles and see if one of those roles has that permission. And it returns a boolean true or false on it's findings. 2 Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now