Show excerpts in archive but protect the individual posts

I need the home page to be visible as it is. But when a person clicks on the Read More link on any blog's excerpt or tries to open a post, this access should only be for Registered/Paid Accounts.
I can see I can hide parts of content of a post using. If I want my existing blog of 2600 posts to be all paid content I would have to go into each post and do this or maybe add the shortcode in the template with some tweaks.

Please add this as feature request.

  • Adam Czajczyk
    • Support Gorilla

    Hi Alim Bolar

    I hope you're well today and thank you for suggesting it!

    I'm not familiar with your setup but it should be currently possible with some custom coding. You'd want to create a basic child theme for your current theme (unless you are already using one) and create two custom page templates in it: one for the homepage and one for the single post view.

    Posts themselves would be not protected at all but then in the homepage template you'd only use

    the_excerpt()

    WordPress function instead of "the_content()" one to display post content and that would only display leads/excerpts.

    In the single post view template you could alter the WP loop to use both "the_excerpt()" and "the_content" functions like this:

    <?php
    the_excerpt();
    echo do_shortcode('[ms-protect-content id="123"]');
    the_content();
    echo do_shortcode('[/ms-protect-content]');
    ?>

    That should display excerpts to everyone and additionally full content only to the members of the membership of ID 123.

    That's a workaround for now that could help so it might be worth a try.

    Also, this thread is already marked as a feature suggestion so other Members of our Community can vote for this feature. Thank you again for suggesting it!

    Best regards,
    Adam

  • Alim Bolar
    • Site Builder, Child of Zeus

    Hi Adam

    Thanks for the update..

    However I am using another approach which I think you had suggested here..

    https://premium.wpmudev.org/forums/topic/membership-2-pro-show-excerpt-read-more-for-posts-in-protected-category#post-1313002

    It seems to work for me.. Should I continue with it? Please do let me know

    I just had one questions though... as you've created one function in the solution for the link above, could you advise the best way forward if I have to use this for multiple subsites and wanted to keep the structure easy to read..

    If I were to create all the categories for different subsites in that category I feel I would get lost a little bit.. so is there another approach to this? Should I create other functions (one for each subsite)? Or is that a ridiculous thought? Please do understand that I am no developer and my head can't get around very complex functions but the simpler ones I can figure out..:wink:..

    So to rephrase my question.. I am using your approach as per the link provided above.. so first thing is if it's fine to continue using it?
    Second question is for different subsites, should I add the categories into the same function and the membership ids in to the same function or should I create multiple functions (one for each subsite)? And if you say yes to this then could you give me a sample function and some direction on how to go about this?

    Third request is please make this a feature asap!!!..:wink:..

  • Adam Czajczyk
    • Support Gorilla

    Hi Alim Bolar

    Yes, that other solution is also fine. It's even easier to implement as it doesn't require creating a child-theme and/or page templates - it's just adding a small bit of code. If it works for you now, there's no reason to replace it with anything else in my opinion :slight_smile:

    Second question is for different subsites, should I add the categories into the same function and the membership ids in to the same function or should I create multiple functions (one for each subsite)? And if you say yes to this then could you give me a sample function and some direction on how to go about this?

    This function - if added as mu-plugin as suggested in that other post - is executed automatically on the main site and each and every sub-site of entire multisite. The only thing that makes it "protect or not" the content is whether a post belong to one of the categories defined in the code or not.

    I wouldn't go for multiple functions instead. But how to expand/modify this code - that's actually a bit more complex question because it depends heavily on the structure of you multisite/sub-sites content and memberships and what content exactly you wish to protect.

    For example:

    - if you have a site-wide membership (so the same membership is available across entire network) and you want to protect all the standard posts, no matter what category they belong to, that function could even be simplified

    - if you only want to protect only posts within selected categories but with the very same membership (that's available everywhere) you would simply need to add categories to the list

    - if you want to protect different categories with different memberships, some conditional code would have to be added...

    and so on. I'll be happy to assist you more with this but it would help a lot if you could actually describe your multisite/content/memberships structure a bit along with "what should be protected with what" :slight_smile:

    Best regards,
    Adam

  • Adam Czajczyk
    • Support Gorilla

    Hi Alim Bolar

    Yes, that other solution is also fine. It's even easier to implement as it doesn't require creating a child-theme and/or page templates - it's just adding a small bit of code. If it works for you now, there's no reason to replace it with anything else in my opinion :slight_smile:

    Second question is for different subsites, should I add the categories into the same function and the membership ids in to the same function or should I create multiple functions (one for each subsite)? And if you say yes to this then could you give me a sample function and some direction on how to go about this?

    This function - if added as mu-plugin as suggested in that other post - is executed automatically on the main site and each and every sub-site of entire multisite. The only thing that makes it "protect or not" the content is whether a post belong to one of the categories defined in the code or not.

    I wouldn't go for multiple functions instead. But how to expand/modify this code - that's actually a bit more complex question because it depends heavily on the structure of you multisite/sub-sites content and memberships and what content exactly you wish to protect.

    For example:

    - if you have a site-wide membership (so the same membership is available across entire network) and you want to protect all the standard posts, no matter what category they belong to, that function could even be simplified

    - if you only want to protect only posts within selected categories but with the very same membership (that's available everywhere) you would simply need to add categories to the list

    - if you want to protect different categories with different memberships, some conditional code would have to be added...

    and so on. I'll be happy to assist you more with this but it would help a lot if you could actually describe your multisite/content/memberships structure a bit along with "what should be protected with what" :slight_smile:

    Best regards,
    Adam

  • Alim Bolar
    • Site Builder, Child of Zeus

    Thanks Adam for the update.. I am in touch with your colleague Juan and exploring the option of S2Member as WPMU Dev has discontinued the Membership Plugin so I am assuming further development might not really be an option..

    If S2Member doesn't work out I will definitely get back to you for my requirements.. but first I think i'll experiment a little with S2Member to check if these features are available out of the box using that plugin..

    Thanks once again.. will keep this post active for a few more days in case I need to get back..

    Have a nice day..:slight_smile:..

  • Alim Bolar
    • Site Builder, Child of Zeus

    I think i will stick with membership pro for now as it seems to be working fine for me..:slight_smile:.. will let you know if i need any further tweaking in the code but for now it looks perfect for me.. thanks again..:slight_smile:..

  • Alim Bolar
    • Site Builder, Child of Zeus

    Hi Adam Czajczyk

    I had a quick query regarding the MU-PLUGIN that you had created which I am using.. you mentioned that it's applicable for the whole installation and hence all sub-sites and so the categories array that we fill up are for all the subsites..
    Now you also mentioned that we could tweak this for our requirement if I could explain to you properly which subsite has what categories etc.. I totally get this and thanks again for the clear explanation..
    Now I need a newbie query out of my head so that I am clearer..:wink:..
    The mu-plugin you've created looks like a function (pardon my discovery that my sound much too obvious to you!)..:wink:.. and I realise that while creating child themes for each sub-site we have the option of adding is styles as well as functions to each child theme..

    #1) In this case can we add your function to our child theme in the functions.php?
    #2) Does it have to be a mu-plugin for the whole site to work the way it does?

    I was hoping this would make it specific for each of my sub-site and would give me more control over each subsite rather than have one function with if and else conditions for each sub-site..

    Please do advise if what I am thinking is completely wrong or is it an option or am I a genius?!..:wink:.. he he.. just kidding.. but once this is clear and if this is not an option (or I am not a genius!) then I will look at the mu-plugin and start figuring out my requirements to share with you.

    Have a nice day..:slight_smile:..

  • Alim Bolar
    • Site Builder, Child of Zeus

    Adam Czajczyk

    I am having some duplication of content on my site with the code below

    if (has_category($categories)) {
    	    $excerpt = wp_trim_words( $content, 55, null);
    		$content =  $excerpt . '[ms-protect-content id="38008,14269"]' . $content . '[/ms-protect-content]';
    		$content .='[ms-membership-login show_note="no" register="yes"]' ;
      }
      return $content;
    }
    
    add_filter( 'the_content', 'wpmu_myms_the_content_filter' );

    not sure if the '$excertpt .' part is the culprit but I thought I'd ask you before I try anything.. basically the first 55 words are being repeated in the content viewed by the logged in user..

  • Adam Czajczyk
    • Support Gorilla

    Hi Alim Bolar

    As for duplication of content.

    Take a look at this lines of code please:

    $excerpt = wp_trim_words( $content, 55, null);
    $content =  $excerpt . '[ms-protect-content id="38008,14269"]' . $content . '[/ms-protect-content]';

    What happens here is:
    - first line takes 55 words out of entire article and stores it in $excerpt
    - second line takes that 55 words ($excerpt), adds opening Membership shortcode to it, adds entire content (including these 55 words) and then adds closing Membership shortcode

    Then the $code containing that all is printed out.

    This can be tweaked a bit in that way that:
    - if a user is not allowed to see entire content, only the excerpt is shown
    - if a user is allowed to see entire content, only the content is shown

    To achieve that, simply replace these two lines with this:

    $excerpt = wp_trim_words( $content, 55, null);
    $content =  '[membership id="38008,14269" access="no" silent="yes"]' .$excerpt . '[/ms-protect-content]' . '[ms-protect-content id="38008,14269"]' . $content . '[/ms-protect-content]';

    This code above wraps $excerpt in additional membership shortcode that's "reverse protection". It means that $excerpt can only be seen by everyone who is NOT a member of any of these two memberships while $content can only be seen by everyone who IS a member of any of these two memberships.

    As for your earlier questions about mu-plugin vs child-themes.

    Yes, you can put the code in the child theme's functions.php file of course. The exceptions are, though:

    1) you'd need to strip the opening php tag from the code

    <?php

    2) if you want a "granular control per site" you would want to make sure that each site has it's own, unique child-theme. I mean by this that the theme might be functionally and visually the same but "physically" must be separate. Otherwise, if two sub-sites are using the very same exact child theme, they'll both be affected by the same "rules" from the code.

    Best regards,
    Adam

  • Alim Bolar
    • Site Builder, Child of Zeus

    Would appreciate if you could resolve this asap.. :slight_smile:.. as the code is live on site..

    And I have another query.. Have asked this question to Patrick Freitas on another ticket and am yet to receive a response.. but my plans for this site as far as Registrations go could be complex and would definitely grow over time .. and as Membership Pro 2 will not have any further development would it be better for me to look at another plugin for membership? Ultimate Membership.. a good option?
    Please do advise.

  • Adam Czajczyk
    • Support Gorilla

    Hi Alim Bolar

    Something's not right.. I did as you told me above and I get the attached result.. you can log in yourself and see this too..

    Have you moved the code to the functions.php from the mu-plugin? If yes, is Membership 2 Pro active on that specific site of the Multisite or is it network-enabled for entire install?

    If it's not that, this would suggest some error (probably a typo or similar) in the code so could you share again your current code - the exact code that you're using now, please?

    but my plans for this site as far as Registrations go could be complex and would definitely grow over time .. and as Membership Pro 2 will not have any further development would it be better for me to look at another plugin for membership? Ultimate Membership.. a good option?

    I'm tempted to say "stay with Membership 2 Pro" but here's the thing: the plugin will probably work fine for quite some times still and we'll also be providing some critical security/compatibility updates via GitHub but in a long-term business perspective - yes, it might actually be a good idea to go for e.g. Ultimate Membership (it's very popular and quite solid plugin of that kind!) now rather than later, when you'll need to deal with things like content/protection and membership conversions, member migration and so on.

    Best regards,
    Adam

  • Alim Bolar
    • Site Builder, Child of Zeus

    Hi Adam

    It's still in mu-plugins.. I haven't moved it to functions.php

    and here's the code

    <?php
    function wpmu_myms_the_content_filter($content) {
    
      $categories = array(
            'business',
            'insight',
            'interview',
    	'interview-1',
            'news',
            'company-profile',
            'designer-profile',
            'events',
            'expositions',
            'happenings',
            'eyewear-collection',
            'fashion',
            'celeb-watch',
            'trends',
            'updates',
            'uncategorized',
            'vision-care',
            'contact-lens',
            'eye-matters',
            'lens-talk',
            'optometry',
            'extra',
            'feature',
            'news',
            'star-scent',
            'fragrance',
            'french',
            'french-oriental',
            'ingredient',
            'oriental',
            'review',
            'interview',
            'perfumer',
            'profile',
            'perfumer-profile'
    
      );
    
      if (has_category($categories)) {
    /*	    $excerpt = wp_trim_words( $content, 55, null); */
    /*		$content =  $excerpt . '[ms-protect-content id="38008,14269"]' . $content . '[/ms-protect-content]'; */
    
    		$excerpt = wp_trim_words( $content, 55, null);
    		$content =  '[membership id="38008,14269" access="no" silent="yes"]' .$excerpt . '[/ms-protect-content]' . '[ms-protect-content id="38008,14269"]' . $content . '[/ms-protect-content]';
    
    		$content .='[ms-membership-login show_note="no" register="yes"]' ;
      }
      return $content;
    }
    
    add_filter( 'the_content', 'wpmu_myms_the_content_filter' );
  • Adam Czajczyk
    • Support Gorilla

    Hi Alim Bolar

    Thanks for sharing the code.

    Actually, the bug was in my code that I shared with you, I just didn't notice it earlier until I saw entire code. Sorry about that.

    Please replace this line

    $content =  '[membership id="38008,14269" access="no" silent="yes"]' .$excerpt . '[/ms-protect-content]' . '[ms-protect-content id="38008,14269"]' . $content . '[/ms-protect-content]';

    with this

    $content =  '[ms-protect-content id="38008,14269" access="no" silent="yes"]' .$excerpt . '[/ms-protect-content]' . '[ms-protect-content id="38008,14269"]' . $content . '[/ms-protect-content]';

    This will fix the "duplicated content" and "visible shortocode" issue.

    the membership ids had been created for 2 subsites but this code seems to be affecting all subsites.. is that how it's supposed to be? Please do advise..

    It's an "mu-plugin" so the code is executed on each and every site. If the plugin is either "by site" enabled or network-enabled but not in a "sidewide mode" - then if there's no given membership on the sub-site, it will indeed affect it and show only the excerpt.

    So what's the workaround then?

    One way is what you actually asked about: move the code from mu-plugin to the functions.php of the theme and make sure that this specific theme is only used on these 2 sub-sites but no other.

    The other way is to modify the code to actually check what sub-site it is. But since you already asked about functions.php, I think that'd be easier. And you'd avoid unnecessary code execution on any sub-sites that do not need that code at all :slight_smile:

    Best regards,
    Adam

  • Alim Bolar
    • Site Builder, Child of Zeus

    Thanks for the update Adam Czajczyk

    Have implement it in the mu-plugin.. will try the functions.php later..

    In the meantime I had one question.. I have purchased the Ultimate Membership Plugin and I need to install it but to experiment with it I don't want to disturn the 2 subsites where I will have the Membership Pro activated..

    Is there a way where I can have Membership Pro on 2 subsites and the Ultimate Membership active on another subsite so that I can experiment and then implement on the 2 subsites where I currently have the Membership Pro?

    When I network activate a plugin, does it become active across all the subsites? This would probably make membership pro and ultimate membership features clash with each other? I'd like to have membership pro active on 2 subsites while I experiment with Ultimate Membership on another..

    Please do advise me if I should try this or it would be wiser that I disable membership pro completely and only use Ultimate Membership for all the sub sites.

    Please do advise.. as I

  • Alim Bolar
    • Site Builder, Child of Zeus

    Hi Adam Czajczyk

    Don't worry about this.. I have gone ahead and uninstalled the Membership Pro plugin and installed the Ultimate Membership Pro.. it is not as simple as the WPMU Dev plugin for sure but it does seem like one that can take care of a lot of issues..

    Thanks again for all your support. Have a nice day..:slight_smile:..

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.