Tips and Tricks HQ Support Portal › Forums › WP eMember › WP eMember Tweaks › eMember – boolean function if user can access category
Tagged: available categories, category permission
- This topic has 10 replies, 4 voices, and was last updated 5 years, 10 months ago by vinay samant.
-
AuthorPosts
-
April 7, 2014 at 12:59 pm #10625photizoMember
I’m trying to develop a “portal” page for the users of our new site to be able to have a one-stop place to look for the services (post categories) their subscription enables them to view. I’m hoping there’s a php boolean function to report whether or not a user has been granted access to a specific category by ID, but I haven’t been able to find it in the code myself.
April 7, 2014 at 2:50 pm #62155wzpModeratorThe granularity of category protection is at the membership level, not at the member level. So, if a member tries to access a page or post in a protected category for which their membership level has not been granted access, eMember will automagically block them.
With that said, suppose you want to only show members content that may potentially direct them to category protected content.
Stuff every membership level can see goes here...
[emember_protected for=2 do_not_show_restricted_msg=1]
Stuff only membership level 2 should see goes here, including links to
category protected pages that only level 2 can see...
[/emember_protected]April 7, 2014 at 3:22 pm #62156photizoMemberUnfortunately, that’s not very intuitive. The pseudocode for what I’m trying to do:
if (user can access category id=”id”) {
display this content
}
I’d even be fine with “membership level can access category id” as a boolean parameter, since I can easily find out the membership level to which the user belongs.
I suppose I COULD write the code to find out if the user has permission to access one post inside of that category, but that’s prone to failure should that post be deleted, moved, or belong to more than one category.
April 7, 2014 at 4:08 pm #62157wzpModeratorAre you trying to determine if the user has access to a category before or after they reach the protected page?
April 7, 2014 at 5:36 pm #62158photizoMemberBefore. I want to know whether or not to even SHOW them the link to that category and not let them see something that, when they click on it, tells them they don’t have permissions to view anything.
April 7, 2014 at 5:39 pm #62159wzpModeratorOkay, how good of a programmer are you?
April 7, 2014 at 6:28 pm #62160photizoMemberNot a pro, but I manage pretty well. Fire away, and if I don’t understand it, at least I’m no worse off than I am now!
April 7, 2014 at 8:25 pm #62161wzpModeratorRemember, I told you the granularity of category protection is only at the membership level. If you have a particular set of protected categories, they must be assigned to a level unto themselves; and then members should be assigned to that level as appropriate.
What I showed you in the second message, is that you would do if you were using the shortcodes. But from what you’ve said, you’re insisting on doing it “the hard way” by using PHP coding instead. So, what you want to do is bypass the shortcodes, and do it entirely in PHP.
********** DANGER WILL ROBINSON — RUNNING WITH SCISSORS AHEAD **********
********** DANGER WILL ROBINSON — RUNNING WITH SCISSORS AHEAD **********
The emember_protected shortcode is processed by the emember_protected_handler() function, defined in the emember_access_checkers.php file.
Now scroll down the function, and you will see how each of the shortcode operands are handled. You want the “for” operand logic. See how it checks the member’s assigned level against the level specified in the “for=X” operand? It does 2 kinds of checks; both for single and for “stacked” membership levels.
My suggestion is to write an entirely new function, in another file, and to not “hack” the existing code.
If you need rescuing, or decide to opt for professional coding help, please complete the Custom Work Order form and someone will get back to you:
April 7, 2014 at 9:34 pm #62162photizoMemberIt wouldn’t be fun if it wasn’t “the hard way”
April 8, 2014 at 3:50 am #62163adminKeymasterHere is another code example that might be helpful for you:
$id = "1";//Post ID in question. The post that you want to check
$emember_auth = Emember_Auth::getInstance();
if ($emember_auth->isLoggedIn()) {
//This user is logged in so lets see if he has access to a category
if ($emember_auth->is_permitted_category($id)) {
//This member has access to this category protected post
//Do something
}
}December 4, 2018 at 6:00 am #62164vinay samantMemberI have similar requirement like this guy. I allow “sale” of multiple categories, out of which user selects some, not all. So when next time he comes, i want to show him only posts from “purchased categories”, not all..
To explain in detail. I have 10 memberships associated with 10 unique categories. Like Parenting, Economics, Literature, Cinema etc. Now people choose any of them randomly as per their choice. Never choose all of them. Lets say, one has selected Economics & Literature then i want to show him articles only from those categories.
This is different that your logic of protection of articles. Here the enduser is already on article and you decide whether to protect or not. In my requirement, we want to present him ONLY posts that he can access. So he dont have to see that protected message any more.
So my requirement is, on one page, i should be able to get list of all categories one has access to, and then pass it to a widget like “category posts” that will present content from select categories..
Currently i use it in following way –
[/emember_protected][emember_protected for=”3″ do_not_show_restricted_msg=”1″][pt_view id=”45dfe8ew17″ field=”id” cat=”4″][/emember_protected]
[emember_protected for=”4″ do_not_show_restricted_msg=”1″][pt_view id=”45dfe8ew17″ field=”id” cat=”6″][/emember_protected]
[emember_protected for=”2″ do_not_show_restricted_msg=”1″][pt_view id=”45dfe8ew17″ field=”id” cat=”1″][/emember_protected]
In my case, i have to do it 10 times if i have 10 memberships. And the page will load 10 sections (if one has those many memberships). Instead i want it like
[emember_protected for=”2,4,8″ do_not_show_restricted_msg=”1″][pt_view id=”45dfe8ew17″ field=”id” cat=”1,6,7″][/emember_protected]
Either you can give me such shortcode. Otherwise atleast let me know names of function that will give me “IDs of Categories” AVAILABLE to currently logged in member. You have that record with membership level already in database..
-
AuthorPosts
- You must be logged in to reply to this topic.