[Membership 2 Pro] Patreon like hiding of Featured Pictures

Visiting a Patreon page you can see lists of free and "locked" posts with a special picture overlay saying sth like "Unlock this post by becoming a patron" - if you click on the pic, then you can subscribe to this special patreon powered artist.
I would like to see this in Membership 2 Pro - so in a list of posts
(1) all posts show up (free and "member only" ones) and
(2) the featured picture of all "members only" posts are hidden and cannot be seen by visitors (but by logged in members) and
(3) on the picture or nearby is a link, where a visitor can subscribe.
Maybe that is already possible - but I could not find any hints how to do this.
Thank you!

  • Ash

    Hello Emanaku

    This is not possible out of the box I am afraid, neither it can be easily done. But, I have sent your issue to our second level support team. I can't promise but if it doesn't take a lot of time, then will send you some custom code. Otherwise, you may need to hire a developer to achieve that. Please note that, they deal with more complex issues, so there might be little delay. We appreciate your patience on this.

    Have a nice day!

    Cheers,
    Ash

  • Emanaku

    I spent already one, two hours yesterday to look at it. It seems to be easy to at least display a "member only" thumbnail (with the link I can deal later) , if I could only find the right function testing if a logged in user is allowed to see a post. But I could not find this function (because there is no documentation of the public PHP functions).

    Here is what I tried:

    add_filter( 'get_post_metadata', 'ema_members_only', 10, 4 );
    function ema_members_only( $feature_image_id = null, $post_id, $meta_key, $single ) {
        // for tests we return id 74 (which is a picture in the WP library with the text "for member only"
    	$ema_members_only_id = 74;
    		if( $meta_key == '_thumbnail_id' ) {
    	  		if( ! has_access_to_post( $post_id )) { // which function to use here?
    	      	 	return $ema_members_only_id;
    	 		}
    	 }
        return $feature_image_id;
    }

    The result is right now: In a listing of posts and custom posts, where some are visible for the user's membershipw, and some are not, either all thumbnails are "members only" or none is.
    Is there a public function doing exactly what I want? And which is it?
    Thank you vey much!

  • Panos

    Hey there Emanaku ,

    The rules work a bit different and we don't have a direct property nor method to check if member has access to post.

    Not sure how Patreon works nor what exactly are your needs. I have prepared a custom mu-plugin which you can download from here :
    https://gist.github.com/wpmudev-sls/58161e33986e124bf839309d99d20d91

    It filters the featured image as you mentioned such a filter in your reply. It also filters the post_link which points to the memberships page in the posts list. You can also filter the_content and add a link to the memberships page too.

    In order to specify the default image id, you need to change this line:
    static protected $default_featured_img_id = 362;

    Hope this helps!

    Please let me know if there is something that is not clear.

    Kind regards!

  • Emanaku

    Thank you so much Panos!
    I made a couple of changes to get it work. See
    https://github.com/emanaku/ema-ms-custom-post-protection/blob/master/ms-custom-post-protection.php
    (sorry I am not very fluid speaking "GITish" :wink: )
    Remarks:
    - in line 85 I added cpt-group, so the custom posttypes would be handled, too (that is where the members only content is)
    - in line 38 (and 49) I added a test, if the user has admin capabilities (because otherwise admins suddenly don't see the members only post any more)
    - most important: in line 42 I added a test of the name of the post type (after debugging $denied_ids and seeing, that it has the post type name, but not single ids of posts)
    I assume (without diving too much into the rules mechanics), that for other kinds of protection (e.g. by category) this part has to be adjusted to cover these other rules.
    With these changes the system does what I want - see screenshot below.
    I did not change the link, because if the page is protected, M2 shows the "Protected Content" page, and there I have added the login shortcode, which also shows a register link.
    I need a bit more testing - like what happens with two or more memberships....
    But for now I am super happy with this solution!
    Thank you again, Panos for your quick answer and great help!
    Emanáku

  • Emanaku

    The "super happiness" did not last very long :wink:
    Consider this case:
    We have content of a farm in the Andes.
    Part of the general content is everything related to Alpacas.
    There are three cases of people:
    - Visitors (see non-restricted posts)
    - Farm friends and family (see everything about the farm)
    - Alpaca lovers (see only the posts related to Alpacas
    The custom post type for "alpacas" is allowed for farm friends as well as alpaca lovers.
    But in the mu-plugin, when creating the "denied IDs", the program denies as an example the custom post type (e.g. for alpacas) for the farm friends, because it is allowed in another membership.
    To deal with this, I have adjusted the github version to use denied as well as allowed IDs. Then a test in the (hopefully) right order seems to produce the desired result....

  • Panos

    Hey Emanaku !

    Glad you managed your way through there :slight_smile:

    We can't really support custom request in such detail so usually we provide general snippets which can be used as a guide and be adjusted by other devs to fit each case's needs.

    I had a look at your gist and it seems good. I noticed that you were trying to debug some parts as I noticed print_r was used. In case you are not familiar with, you can use the error_log function. So if the WP_DEBUG_LOG is defined as true, it would print your results in the debug.log file. Or you can chose to print on a different file.

    Kind regards!

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.