Home › Forums › Product Support Forums › Ajax Search Pro for WordPress Support › Restrict Results Based on Paid Memberships Pro Level
- This topic has 11 replies, 2 voices, and was last updated 1 year, 11 months ago by
Ernest Marcinko.
-
AuthorPosts
-
June 20, 2024 at 2:46 pm #48528
bdkelly
ParticipantHello,
We have a membership site that we utilize Paid Memberships Pro on. Content is restricted using that plugin based on the membership level. We’d like for Ajax Search Pro to be able to restrict results based on the membership level as well. For example, we have three levels: Side A, Side B, and Both. If someone subscribes to Side A, we don’t want them to be able to see content in search from Side B.
Prior to purchasing, I sent a question and was told it should be simple enough to create a snippet in order to implement, based on the PMPro list of filter hooks, etc.
Is this something that you all can create/implement in the plugin?
Thank you,
Brendan KellyJune 21, 2024 at 12:43 pm #48533Ernest Marcinko
KeymasterHi Brendan,
I recall answering your question about that. Does that plugin have an API documentation which I can take a look? I couldn’t find anything meaningful with a quick google search. I looked at past support tickets, but apparently nobody had a similar query yet.
All we need is a simple method to check if the current user has access to a resource. I assume there is a very simple way – perhaps a function, hook, Class or anything similar.
June 21, 2024 at 2:20 pm #48535bdkelly
ParticipantI think they have the right documentation for you. I’ve logged in and saved several pages as .mhtml files so you can see the whole page without logging in. Take a look and let me know if any of these will work for you.
June 21, 2024 at 2:28 pm #48537Ernest Marcinko
KeymasterThanks!
The pmpro_hasMembershipLevel looks promising, but still need a way to check what level the current content is. I guess each post/item has a level and there is some way to retrieve that by the post ID?
With that a possible solution would be to:
– go through all the results in a loop
– get the access level of the post by it’s ID
– check if the user has access to that level
– remove it from the results if notThe only thing missing here is how to get the content level by it’s ID.
June 21, 2024 at 3:29 pm #48538bdkelly
ParticipantWe have our site set up kind of in reverse, where the post/page visibility isn’t restricted by the level, but the member is restricted from seeing content by their level. We have two categories of content, and users can see one, the other, or both depending on their subscription. So I’m not sure getting the access level of the post/page works, but will leave that to you and your expertise.
As I’m writing this out, I’m thinking we do have the posts/pages assigned to two different categories – A and B. Then PMPro determines what categories the membership level can access. Would pulling the category of the post/page and then checking it against the membership level work?
They do have a get membership level filter. Would this help?
pmpro_get_membership_level_for_user
Return the first active membership for a user.
/** * Retrieves the first active membership for a user. * * @since 1.8.5.4 * * @see pmpro_getMembershipLevelForUser * * @param object $level Level object. * @param int $user_id The User ID. * * @return object $level Level object. */ apply_filters( ‘pmpro_get_membership_level_for_user’, $all_membership_levels[ $user_id ], $user_id );Parameters
$level
object
$user_id
intReturn Value
$levelSource
File: https://github.com/strangerstudios/paid-memberships-pro/blob/master/includes/functions.phpJune 23, 2024 at 1:43 pm #48543Ernest Marcinko
KeymasterOkay, now I understand it!
Can you please share the membership levels and the category IDs they can or can’t access?
With that I can probably construct something to add an exclusion to the query – by getting the user level and then appending the categories to exclude.June 24, 2024 at 2:45 pm #48552bdkelly
ParticipantYou cannot access this content.
June 25, 2024 at 9:58 am #48560Ernest Marcinko
KeymasterPerfect!
Okay, so based on all the information I got this:
add_filter( 'asp_query_args', function ( $args ) { $level_tag_map = array( 2 => array( 33 ), 3 => array( 34 ), ); $user_level = pmpro_getMembershipLevelForUser(); if ( $user_level === false ) { return $args; } if ( isset($level_tag_map[ $user_level->ID ]) ) { $args['post_tax_filter'][] = array( 'taxonomy' => 'post_tag', 'include' => $level_tag_map[ $user_level->ID ], 'allow_empty' => false, ); } return $args; }, 10, 1 );I couldn’t test it obviously, so please be careful.
Try adding this code via the Code Snippets plugin or to the functions.php file in your theme/child theme directory – make sure to have a full server back-up first for safety. For more details you can check the safe coding guidelines.
June 25, 2024 at 3:31 pm #48577bdkelly
ParticipantThank you! I appreciate the effort that has gone into creating this for us.
I’ve added and tested it and users that have access to both areas can get results, but users with access to only one area aren’t getting results at all, within their area or not.
June 25, 2024 at 3:44 pm #48579Ernest Marcinko
KeymasterThank you for the feedback!
I suspect the taxonomy might not be
post_tagbut something else. Can you please check the taxonomy name? You can see it when you open the category editor for that content: https://i.imgur.com/KHD90A8.pngJune 25, 2024 at 4:23 pm #48583bdkelly
ParticipantPerfect! You were right, I changed post_tag to category, and it looks like we’re good to go now. I’ll be back in touch if we run into anything else.
Thanks again!
June 25, 2024 at 5:27 pm #48584Ernest Marcinko
KeymasterGreat! 🙂
Let me know if there is anything.
If you like the plugin and have not rated already, feel free to leave a rating on your codecanyon downloads page and on the wordpress plugin repository, it’s greatly appreciated.
-
AuthorPosts
- You must be logged in to reply to this topic.