This website uses cookies to personalize your experience. By using this website you agree to our cookie policy.

Restrict Results Based on Paid Memberships Pro Level

Home Forums Product Support Forums Ajax Search Pro for WordPress Support Restrict Results Based on Paid Memberships Pro Level

Viewing 12 posts - 1 through 12 (of 12 total)
  • Author
    Posts
  • #48528
    bdkellybdkelly
    Participant

    Hello,

    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 Kelly

    #48533
    Ernest MarcinkoErnest Marcinko
    Keymaster

    Hi 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.

    #48535
    bdkellybdkelly
    Participant

    I 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.

    #48537
    Ernest MarcinkoErnest Marcinko
    Keymaster

    Thanks!

    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 not

    The only thing missing here is how to get the content level by it’s ID.

    #48538
    bdkellybdkelly
    Participant

    We 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
    int

    Return Value
    $level

    Source
    File: https://github.com/strangerstudios/paid-memberships-pro/blob/master/includes/functions.php

    #48543
    Ernest MarcinkoErnest Marcinko
    Keymaster

    Okay, 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.

    #48552
    bdkellybdkelly
    Participant

    You cannot access this content.

    #48560
    Ernest MarcinkoErnest Marcinko
    Keymaster

    Perfect!

    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.

    #48577
    bdkellybdkelly
    Participant

    Thank 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.

    #48579
    Ernest MarcinkoErnest Marcinko
    Keymaster

    Thank you for the feedback!

    I suspect the taxonomy might not be post_tag but 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.png

    #48583
    bdkellybdkelly
    Participant

    Perfect! 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!

    #48584
    Ernest MarcinkoErnest Marcinko
    Keymaster

    Great! 🙂

    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.

Viewing 12 posts - 1 through 12 (of 12 total)
  • You must be logged in to reply to this topic.