[Membership 2 Pro] Show excerpt + read more for posts in protected category

Hi,
I would like to hide articles in most categories but show the first few lines (before the "Read more" tag), a bit like the ft.com does. I cannot see a way of doing this.

All I have been able to do is hide the rest of the content manually using shortcodes, but this is a pain for nearly 10 years of content (I'm about to relaunch a static html site as a membership site - the url I've given is the old one, I'm working on the new version separately).

Is there a way of only applying the Read More protection for certain categories? If not, a) would it be too complex to provide some code for me to use in the plugin and b) could you add this as a feature request?

Many thanks,
Arthur

  • Adam Czajczyk

    Hi Arthur

    I hope you're well today and thank you for your question!

    You can achieve this with a small bit of additional code. Take a look below please:

    <?php
    function wpmu_myms_the_content_filter($content) {
    
      $categories = array(
    	'google-news',
    	'something-else'
      );
    
      if (has_category($categories)) {
    	    $excerpt = wp_trim_words( $content, 55, null);
    		$content =  $excerpt . '[ms-protect-content id="1234"]' . $content . '[/ms-protect-content]';
      }
      return $content;
    }
    
    add_filter( 'the_content', 'wpmu_myms_the_content_filter' );

    This hooks to a native WP "the_content" filter, then checks if the post is in one or more of defined categories. If not, it simply returns unaffected original content.

    Otherwise, it first creates a 55 words long excerpt out of the content and then alters the content in the form of:

    excerpt + MS protection shortcode + original content + closing MS protection shortocde.

    To use it on your site:

    - first create an empty file with a php extension (e.g. "ms-protect-my-categories-posts.php")
    - copy and paste code from above to that file (using some "clean text" editor such as e.g. Notepad++, Sublime or similar)
    - define your categories in this part:

    $categories = array(
    	'google-news',
    	'something-else'
      );

    use category slugs here; I've set two categories there but you can add more if you want

    - define which membership should be used by replacing 1234 with an ID of the membership you want to use in this line:

    $content = $excerpt . '[ms-protect-content id="1234"]' . $content . '[/ms-protect-content]';

    to find an ID simply check ID in shortcodes on "Membership 2 -> Memberships" page

    - upload the file to the "/wp-content/mu-plugins" folder of your site's WP install (via FTP or cPanel); if there's no "mu-plugins" folder inside "wp-content" folder, just create an empty one.

    I have tested this on my own setup and it seems to be working fine, both on post lists (category archive) pages and on single posts.

    Best regards,
    Adam

  • Arthur

    Hi Adam,

    Thanks for the detailed help, that's awesome.

    The only problem is that the content protection message is repeated - perhaps because I've got multiple membership IDs in the "allowed" types. And since the bold markup is lost (which I'd prefer to keep anyway, but it's a minor thing), it's difficult to read. I had also added a signup link shortcode to the original read more message, which isn't showing.

    More significantly, though not related directly to the specific request I originally made, I have just realised that the whole category protection isn't working how I would like anyway.

    Basically, having recreated all this content and categorised and tagged it, the idea was to have the bulk of the archives available to members only (starting at free - I would just like the ability to communicate with my readers in return for all our work!). But to allow people to get a taste of what's in the rest of the site, the idea was also to have all the latest articles available without registering.

    What I thought I could do, therefore, is add posts temporarily to the "Latest" category in addition to the actual meaningful category - such as "Ethics". But it clearly works on the basis that if it belongs to a protected category it will be hidden, regardless of whether it also belongs to a non-protected one.

    I can understand that logic, but hopefully you can understand mine too in wanted to create a positive user experience while balancing free and accessible content with the need to have some sort of benefit (for the site as a whole), in a way which grows the community. I know I could simply un-check the other categories, but that loses the whole benefit of categorisation in the first place.

    Thoughts?

    Thanks again for your great, clear code and explanations.
    Arthur

    First of all,

  • Adam Czajczyk

    Hi Arthur

    Thank you for your response!

    The only problem is that the content protection message is repeated - perhaps because I've got multiple membership IDs in the "allowed" types.

    No, that shouldn't be happening due to the multiple memberships used for protection. I've just tested in on my site again with two memberships and there's only a single protection message.

    The two requirements are:

    1. all memberships must be withing single shortcode so in my code you'd use it like this:

    $content = $excerpt . '[ms-protect-content id="1, 2, 3, 6"]' . $content . '[/ms-protect-content]';

    where 1, 2, 3, 6 are IDs of memberships, separated by commas.

    2. The categories protected by this code (as well as posts within them) cannot be protected by "Membership -> Protection Rules".

    Also, maybe there are already shortcodes used within these posts? I mean - added via WP post editor?

    More significantly, though not related directly to the specific request I originally made, I have just realised that the whole category protection isn't working how I would like anyway. [...] What I thought I could do, therefore, is add posts temporarily to the "Latest" category in addition to the actual meaningful category - such as "Ethics". But it clearly works on the basis that if it belongs to a protected category it will be hidden, regardless of whether it also belongs to a non-protected one.

    Yes, that makes sense but it actually won't quite work this way. Regardless whether you use "category protection" (Membership 2 Pro -> Protection Rules) or my code, the point is that when the user visits the post and the plugin detects that the user doesn't have "right to access the category of the post" (any of the categories) it will protect it. It's a bit difficult to explain but it's just hard to make it work other way, it would be a bit of a "Schrödinger's cat" situation when the post is at the same time protected and not protected. I believe you know what I mean :slight_smile:

    I'm thinking, however, of yet another solution. Using my code - if the post was not protected at all - we could actually check the publication date and maybe do some simple calculation (like if the post is never than 7 days, show it) to "conditionally apply" the protection shortcode. Take a look at hist adjusted version of my previous code please:

    <?php
    function wpmu_myms_the_content_filter($content) {
    
      // articles from these categories shoudl be protected
      $categories = array(
    	'google-news',
    	'something-else'
      );
    
      $max_age = 7; // don't show articles older than this number of days
    
      #### END OF SETTINGS ###
    
      $protected_date =  time() - ( $max_age * 86400 ); // calculate date for newest protected post (unix timestamp)
    
      if (has_category($categories)) {	
    
    		$post_date = get_the_date( 'U', get_the_ID()); // get publication date of articles
    
    		$pp = '<strong>P: ' . $post_date . 'M: ' .$protected_date. '</strong><br /><br />';
    
    		if ( $post_date < $protected_date ) { //if post is older than calculated protection date, protect it
    
    			$excerpt = wp_trim_words( $content, 55, null);
    			$content =  $pp . $excerpt . '<p class="custom-protection">[ms-protect-content id="208276,208277"]' . $content . '[/ms-protect-content]</p>';  
    
    		}
    
      }
      return $content;
    }
    
    add_filter( 'the_content', 'wpmu_myms_the_content_filter' );

    This should keep all the articles from those two protected categories open if they were published within last 7 days and if they are older, they would be protected. I also enclosed the "protection message" part in an additional <p> element of ".custom-protection" class so you can user regular CSS to style it to stand out a bit.

    I'm not sure however what do you meant by this:

    I had also added a signup link shortcode to the original read more message, which isn't showing.

    Is it that you just want to display some login/registration/signup link next to the protection message if the article is protected?

    Best regards,
    Adam

  • Arthur

    Hi Adam,

    Wow, that's a heavyweight answer - thanks!

    id="1, 2, 3, 6"

    I was adding the two IDs separately (id="1", id"2"). I've changed it and the duplication is gone. :slight_smile:

    Re 2, I was aware of the shortcode overriding the Protection Rules function, yes.

    I like your Schrödinger's cat analogy and yes, I do see what you mean. And I love your solution even more. Setting the time for protection is a genius solution and gets me what I want. I'd pass that onto the plugin developers to add it as a feature, it would be great: I'd suggest having options like # of days, weeks or months (even years?).

    I would also suggest adding "number of posts" so we have the option of setting protection by number rather than date (I'm assuming this would be as simple as the date calculation, by counting the number of published posts and fetching their IDs to protect. This would allow more flexibility. (The alternative, of course, is to be more disciplined at generating content! But it would be closer to my original idea.

    Embedding this in the code itself could potentially allow it to be integrated with the Protection Rules again to make the whole thing as simple as it is now (it's SUCH a great plugin for that (and the rest, of course!)...child's play).

    I'll check it out, including adding some CSS as you say, and let you know.

    Is it that you just want to display some login/registration/signup link next to the protection message if the article is protected?

    Exactly, that's what I meant/had done.

    Thanks so much again,
    Arthur

  • Arthur

    Ok, I'm getting a few weird things now.

    1. Having set the protection time to 1 year (the posts are fewer but longer), I am now getting the perfect result of bold excerpt plus signup link...but for the latest posts (within 365 days)!

    2. Older posts are showing up with a weird code, also in bold: e.g. P: 1486644802M: 1492023783, then the excerpt (set by the character limit, not the Read More tag), not in bold, followed by the protection message and no signup link.

    3. One one post I can see the whole article, though with the same P code and protection message.

    4. When I remove a category from the protection list, it's still protecting it (even after clearing cache and restarting the browser).

    We seem to be so close yet so far. If I can have the older posts behaving like the newer ones and vice versa, and stop protection on the non-listed categories, things would be perfect!

    Cheers,
    Arthur

  • Adam Czajczyk

    Hello Arthur

    1. Having set the protection time to 1 year (the posts are fewer but longer), I am now getting the perfect result of bold excerpt plus signup link...but for the latest posts (within 365 days)!

    We're definitely getting somewhere but I must be missing something (probably quite simple, as simple things are usually most difficult to spot:stuck_out_tongue:). Anyway, I have tested the code again on my setup: I've set to 1 year ($max_age = 365:wink: and all the post with the year back from today were displayed "open" while posts older than a year were displayed as a 55-words long excerpt and a protection message.

    One thing that might be the clue here: you said "perfect result of bold excerpt + signup link" and previously you said that you have added signup link to "read more" text. What I would like to make sure of:

    - are these posts (within last 365 days) "open" for visitors (with my code applied)? I mean, when a visitor (not logged in user) enters the post directly, can he/she read entire post?
    - then what do you get for older posts - is there short excerpt and protection message and when visitor (non-member) tries to read it, they can't?

    If so, that would follow my intention but I might still be missing your goal. I thought that some new(er) posts should be completely open and only older should be "excerpt + protection" but maybe you meant that those within 365 days should only show "excerpt + signup link" but full post shouldn't be accessible? If so, then what with older ones? I"m sorry if I'm "messing that now" but I got a feeling that I missed the point but we're very close to what it should be :slight_smile:

    If you could maybe just tell me exactly by examples:

    1) assuming I'm a visitor (not logged in user or not a member) and I visit a post list:
    - what should I see for < 1yr old posts?
    - what should I see for > 1yr old posts?

    2) I'm also a visitor but I visit post directly (single post page):
    - what should I see for <1yr old post?
    - what should I see for >1yr old post?

    That is, assuming that these posts fall under protected categories, of course :slight_smile:

    2. Older posts are showing up with a weird code, also in bold: e.g. P: 1486644802M: 1492023783, then the excerpt (set by the character limit, not the Read More tag), not in bold, followed by the protection message and no signup link.

    That's my mistake, sorry. I forgot to remove some "debugging" lines from the code before sharing. I'll remember about it for future and as for the current code, simply remove this line:

    $pp = '<strong>P: ' . $post_date . 'M: ' .$protected_date. '</strong><br /><br />';

    3. One one post I can see the whole article, though with the same P code and protection message.

    How long is that article? Could you share a link to it with me so I could take a look?

    4. When I remove a category from the protection list, it's still protecting it (even after clearing cache and restarting the browser).

    By any chance, are those posts that are still protected in more than one category, where one of these categories still does fall under protection? That might explain it and if so, it should be fairly easy to change... :wink:

    As for that all being a feature. Yeah, I fully agree that it would be a nice feature. I can say already that this particular code (even after we make it fully suit your needs) cannot be incorporated into the plugin "straight away" because it's based on "different principle" but of course there are other ways to achieve that to so once we're done with this I can move this thread to our "Features and Feedback" forum section as a feature request, giving other Members of our community a chance to vote for it. Or you can simply start a separate one in "Features and Feedback" (that might be even better as it would be more "clearly stated") suggesting such sort of protection.

    There is a "dripped" type of membership built in which actually is very similar thing, especially as it can be set relative to the moment a member subscribed but it's not exactly the same (no excerpt show, no "relative to content publication") so what we're discussing here could nicely supplement it :slight_smile:

    Best regards,
    Adam

  • Arthur

    Hi Adam,

    You understood correctly, so there must be something in my setup/something I did before this that's causing trouble. Part of the problem, I've just realised, is that only the last couple of years' posts have the read more tag - these were imported from a separate blog I set up, the rest were created from static html files.

    I'll confirm your understanding in detail:

    1) assuming I'm a visitor (not logged in user or not a member) and I visit a post list:
    - what should I see for < 1yr old posts? all posts
    - what should I see for > 1yr old posts? all posts

    I was planning on all the posts being visible in all lists, just not readable:

    2) I'm also a visitor but I visit post directly (single post page):
    - what should I see for <1yr old post? everything (no protection message)
    - what should I see for >1yr old post? excerpt (in bold) + protection message + register link

    What I am currently getting is EVERYTHING is protected!

    Edit: everything in the protected categories - I can now see all the non-protected category posts and the problem with the individual post being visible is now working correctly. So it's just the protected categories that have the problem of them all being protected - the difference being that those less than a year have the "pretty" message with the signup link and the older ones without.

    I've updated a publicly available test site so you can see what I mean:

    New article (in a protected category):
    http://www.imustardtest.co.uk/latest/holistic-corporate-governance-can-save-the-audit-profession/

    Older article (in a protected category):
    http://www.imustardtest.co.uk/golden-rules/ethics/corruption-and-corporate-governance/

    By any chance, are those posts that are still protected in more than one category, where one of these categories still does fall under protection? That might explain it and if so, it should be fairly easy to change...

    This has got me thinking. There may well be older articles that are in both protected and unprotected categories - thanks for pointing that out! So I may have to rethink. The sensible thing would be to add a members (protected) category instead of a latest category - especially since WordPress has the "recent posts" facility built in, making a "latest category" all but redundant; I put it in as my theme has a cool way of displaying categories and it gave me more freedom, but I've just realised that it works for recent posts too.

    This means that after a certain time, I can add a post to the protected "members" category and it will protect it. Doh!

    I would, however, still like to get the excerpt working, though, so your code will still be very helpful - but maybe we don't need the time thing - it's a nice option for automatically protecting content, but perhaps the less we touch the plugin the better.

    I think the new logic is much more in line with the way the plugin was developed, so hopefully it will be easier to integrate as an option. If necessary (since it's simpler), once we're done here, I will indeed add a new feature request with the simple protection/read more link based on protected category (which I assume could be done in line with Protection Rules) - if I have time, I'll add a detailed spec and use cases for the time-based version.

    This has reminded me (as if I needed it!) of the complexities of software development, but also of the cool results a good brainstorm can bring. I particularly like that, unlike my previous life as an owner, the process and these results are publicly available and benefit everybody!

    Cheers, Adam
    Arthur

  • Arthur

    I've enabled support access on the test domain above. It's getting stranger, but I think there's a caching issue going on too, as it's different on different browsers. I've tried disabling the custom protection messages to see where it's getting the message from (which is still repeated in some places.

    Anyway - have a look.

    In passing, I also have a weird issue of redirection on login, see: https://premium.wpmudev.org/forums/topic/membership-2-pro-redirect-to-website-builder-on-login-membership-2

    I just mention that as perhaps you can look at that too while your there.

    Cheers,
    Arthur

  • Adam Czajczyk

    Hi Arthur

    Thanks for your elaborate response, I think it's must clear now, especially if it comes to how this protection should work.

    In fact, what I gave you works in a bit different way so no wonder you couldn't achieve desired effect :slight_smile: If I'm not mistaken there'll have to be two - not that complex - changes made to 1) make it protect only single post pages for older posts and 2) to actually *not overlap categories*.

    There are two issues though: thanks to the fact that you set a test site and enabled support access to it, I was able to take a look there and I must say I'm not quite sure if the code, even if its current form, actually does work properly (in that sense that "the way I made it") there due the fact that some other plugins might also put some filters on "the_content()" (in fact, it seems that theme does that as well). If I'm right, solving that would mostly be a matter of setting correct "priority" for my action hook but I'd need to do it "experimentally".

    Also, could you tell me please how/where did you add that signup link (code-wise)? Is it somewhere in theme files or in my code?

    Since it's a test site, I think you might be able to safely let me access it via FTP - would that be possible? I'd like to give it yet another try and if I could work directly on that site, it would be great.

    If that's fine, please send in:

    Subject: "Attn: Adam Czajczyk"

    - Mark to my attention, the subject line should contain only: ATTN: Adam Czajczyk
    - Do not include anything else in the subject line, doing so may delay our response due to how email filtering works.
    - Link back to this thread
    - login URL and admin account login credentials (may be a temporary admin account) data
    - Include FTP log-in details (hostname, username & password)
    - Include any relevant URLs for your site

    Please use our contact form here https://premium.wpmudev.org/contact/#i-have-a-different-question

    Just don't remove the mu-plugin that we came up with so far from the site (let it stay there in its current form) :slight_smile:

    As for caching, I'd look at that as well.

    As for that login redirect issue. I created a subscriber account for myself and tested it - just like Kasia did in the original ticket, via "/account" page, via the login widget in sidebar and via /wp-admin page - and in all cases I had no unexpected redirects.

    If there's any "special" way to replicate this or it applies to specific user accounts only, please let Kasia know in that other thread and we'll look at it again :slight_smile:

    Best regards,
    Adam

  • Arthur

    Message sent - thanks so much Adam, you guys are the best.

    As I said in the message, feel free to poke and prod around as much as you like on the test site - I hope it helps others facing a similar issue.

    Re the signup link, I just used the "subscribe button" shortcode for one of the memberships and added it to the custom message (Gold in the first instance...which is now "inactive" as I'm not ready but I've since disabled the custom messages).

    Cheers,
    Arthur

  • Adam Czajczyk

    Hello Arthur

    I received your message, thank you very much.

    I checked the site and the code and I'm still a bit confused because of how you configured my code. You gave me these two articles as examples:

    1. http://www.imustardtest.co.uk/latest/holistic-corporate-governance-can-save-the-audit-profession/

    2. http://www.imustardtest.co.uk/golden-rules/ethics/corruption-and-corporate-governance/

    The first one is younger than a year and both the excerpt and entire article should be visible to everyone, if I correctly understand. The 2nd one is older than a year and only the excerpt should be visible to everyone while entire article should be visible only to members. Correct?

    The code on site was configured to a) affect only category "archive" while those posts are in "latest" and "ethics" and b) the $max_age was set to.... 10065 days which is roughly about 27 1/2 years :slight_smile:. So, in theory everything should be open. But everything was closed due to yet another setting in Membership 2 Pro: you had "read more" protection set so if you are using "read more" in article and that protection is set, the articles where "closed" for everyone. That's not "time relevant" and my code was fully overwritten by this.

    I've made four changes in the code (take a look inside please):

    1) I've set following categories to be protected (I took them from example articles)

    'latest'
    'ethics'

    2) I've set max_age to 365 days, meaning that posts older than this would be protected but not newer

    3) I've added a line that makes the code being applied only to the "single post view", so post list view remains untouched - now everyone can see posts, featured image and an excerpt on post list but only if they enter the post they can see it or not depending on a membership and post age

    4) I have added [ms-membership-signup] shortcode so if a post is protected and visitor can't see it, they got buttons to by memberships.

    To make it work together I have also disabled "Read more/more tag" protection.

    Can you now

    a) visit both example posts as visitor then as member and see how they behave? I think that's what you wanted or at least pretty close (apart from visual aspects but those are mostly CSS that can be addressed later)

    b) visit post list pages for /latest and /ethics category to see how they behave? You should get excerpts there for everyone.

    Best regards,
    Adam

    'archive'

    10065

  • Arthur

    Hi Adam,

    First of all, apologies for the state I left the code/site in - I thought I'd cleaned it up after my playing around with it but clearly not.

    I thought we were close, but I think we need to go back to basics again as it's getting confusing. I started writing and testing and discovered lots of little things that don't make sense, like the automatic excerpts (from your code) appearing (though with no signup link) before the complete article - in a protected category. Many do work fine, but something is causing strange behaviour. This is probably due to a) the content comes from both static html and a newer WordPress blog and b) my playing with settings and individual posts to try and get things to work how I want.

    So rather than go on about problems, I'll set out what I think we should do, which is a bit different from my original request, but I alluded to it in my last post. I think it's a lot simpler:

    1. Protect ONE category: Members' Archive

    2. Manually add articles to be protected to the Members' Archive category.

    3. Use a code snippet to extract either what's before the "read more" tag or an arbitrary excerpt (e.g. 55 words)*

    Having done the first two (it's taken me an hour or two to write this reply, between testing your scenario and trying my new one!), and with your code in place, everything is now working as I want it, subject to the CSS tweaks, which, as you said, we can do after.

    Having used the Archive to protect older posts, the time/day function in your code is effectively unused, but a) it means I can keep certain posts in protected categories open (such as those in my Editor's Picks), making this approach more flexible and b) I still think this is a great feature to be added to the core plugin code. I'm sure that without my tweaks which conflict with your code, having this in the plugin itself, would make it work nicely.

    Sooo...

    For me, all that we need to do is allow for formatting in the automatic excerpt. I'm not sure how this could work, as it implies extracting it from the post. Apart from bold formatting for the manual excerpt, there are a few places with links, for example:

    http://www.imustardtest.co.uk/archive/risk-management-and-corporate-governance-topical-or-typical/

    where I would be happy to have the infographic available to visitors as it's a useful, shareable asset. However, this is a nice-to-have. I'll be happy with a bit of bold and some paragraphing, if possible.

    Thanks again!
    Arthur

    * could be a nice option to have both if and when this makes it into the core plugin code, to have more control - especially in themes where the manual excerpt is prominent, like Twenty Sixteen - or let the system do it for you.

  • Adam Czajczyk

    Hello Arthur

    Thank you for your response.

    If I correctly understand, that's now actually working pretty much as you wanted it except HTML formatting in excerpts, right?

    That might be a bit "tricky" as WP is automatically stripping all HTML tags and ignoring shortcodes in excerpts. I think we can try to overcome the first issue but I would strongly suggest sticking to "ignoring shortcodes", especially because you seem to be using visual builder on site - executing shortcodes in excerpts might lead to unbelievable mess, I'm afraid.

    We can try with formatting though :slight_smile: To give it a go, please replace this line in the code

    $excerpt = wp_trim_words( $content, 55, null);

    with this one (it's kind of a "hacky trick" but should be working, at least seems to be working in my tests):

    $excerpt = force_balance_tags( html_entity_decode( wp_trim_words( htmlentities( wpautop( $content ) ), 55, null) ) );

    Give it a try and let me know please if that helped.

    Best regards,
    Adam

  • Arthur

    Hi Adam,

    All working nicely now (as is WPMU Dev, I see, after the outage!). I had to wait for the CDN to update (I thought it wasn't working at first, but then it suddenly changed!).

    So I think I'm finally ready...drumroll...to launch! It's been a long, at times difficult journey, but I'm really pleased with the result.

    Thanks again for your help, I'll update this one more time when the real page is live so we can all celebrate :grinning:

    Cheers,
    Arthur

  • Arthur

    Weeellll...the site is now live! http://www.applied-corporate-governance.com

    Inevitably I can see a few things I need to work on, but it's generally good.

    One of those things I would like to fix is that on the protected pages, the signup options are still there. While you could argue it's good to have the upgrade option if users are on the free membership, you shouldn't see a description of your own membership (and on my theme the buttons are really small so it kind of looks like you haven't signed up or something's wrong).

    Is it possible to hide the membership list/description on login?

    Cheers,
    Arthur

  • Arthur

    Ah, sorry, a case of assumptions, which as my old project manager used to say, make an "ass" out of "u" and "mi"...

    Try this one, for example - I assume you can see the new site already, but it may still be propagating where you are...

    https://www.applied-corporate-governance.com/board/ceo-mot/

    Basically, I am only protecting the /archive/ ("Members' Archive") category, to allow categories to have both protected and unprotected content, depending on my goals or the subject matter.

    Thanks!
    Arthur

  • Predrag Dubajic

    Hi Arthur,

    Unfortunately, support access doesn't work at the moment, probably due to the DNS changes, can you try revoking the access and then re-granting it, that should do the trick.

    Buttons can be easily styled with some CSS like this:

    .single-post .ms-signup-button {
        padding: 10px 20px;
        background: #121212;
        color: #fff;
        font-size: 15px;
        font-weight: 600;
        border: none;
    }

    But I would like to check your setup first to better understand what's going on :slight_smile:

    Best regards,
    Predrag

  • Adam Czajczyk

    Hi Arthur

    Thanks for taking care of access.

    I checked the site and I think I see what you mean. It would be best, in my opinion, if it worked the way that:

    1. If an admin or a logged in member of a membership (the membership that's protecting that content) is viewing the page, there'd be no "sign up" part at all - it's not necessary

    2. If a logged in user that's not allowed (not a member of any of these memberships) or a visitor is viewing that page, they do see the "sign up" part with nicely outlined buttons (that suit the design of your site).

    Am I thinking right? The buttons - that we can indeed address with CSS. However, there'd still be a change in the code necessary so I'd need a very current, exact version of the code that we have worked out so far that's used on your site. The simplest way would be if you simply either put the entire .php file (probably .zip archived would be best) on some file storage like your Google Drive or Dropbox and or put it on pastebin.com service and share a link with me.

    I'll then make changes to the code and share it back with you and when we got that sorted, I'll take another look at example page and help you with CSS for the buttons. ok?

    Best regards,
    Adam

  • Adam Czajczyk

    Hi Arthur

    Thanks for sharing the code, I just wanted to be sure that I'm working on the most up-to-date version :slight_smile:

    I've made some additional changes to the code (and also cleaned it up a bit) so here it is:

    <?php
    function wpmu_myms_the_content_filter($content) {
    
      ### CONFIGURATION ###	
    
      // articles from these categories should be protected
      $categories = array(
    	'archive'
    
      );
    
      $memberships = array(
    	'2766',
    	'2767',
    	'3183',
    	'3185',
    	'3090'
      ); // memberships to be used for protection
    
      $words = 125; // excerpt length (number of words)
    
      $max_age = 365; // don't show articles older than this number of days
    
      #### END OF CONFIGURATION - nothing to edit below ###
    
      if ( is_single() ) {
    
      $protected_date =  time() - ( $max_age * 86400  ); // calculate date for newest protected post (unix timestamp)
    
      if (has_category($categories)) {	
    
    		$post_date = get_the_date( 'U', get_the_ID()); // get publication date of articles
    
    		if ( $post_date < $protected_date ) { //if post is older than calculated protection date, protect it
    
    			$excerpt = force_balance_tags( html_entity_decode( wp_trim_words( htmlentities( wpautop( $content ) ), $words, null) ) );
    			$content =  $excerpt . '<p class="custom-protection">[ms-protect-content id="' . implode( ',', $memberships ) . '"]' . $content . '[/ms-protect-content]</p>';
    
    			// if not not logged in or logged in but not a member, add membership signup
    
    			if ( !ms_has_membership( ...$memberships ) )
    				$content .= '<p>[ms-membership-signup]</p>';  
    
    		}
    
      }
    
      }
      return $content;
    }
    
    add_filter( 'the_content', 'wpmu_myms_the_content_filter' );

    That should do the trick. Also, as you can see, I've "moved" all "configurable" options to a separate block at the start so you could easily set the categories, memberships, excerpt length and (if necessary) "age" there in one place :slight_smile:

    The code should show "sign up" part only if a "reader" is a "visitor" (is not logged in to the site at all) or if is logged in but is not an admin and not a member of any allowed memberships. If a user is logged in but is an admin or a member of any allowed membership - the "signup" part is not displayed at all :slight_smile:

    Give it a try, please, and if it's fine let me know and we can address the "look" part with CSS (just let me know how would you like those parts to look like:
    - the "protection message"
    - the membership title
    - the membership description (if should be visible)
    - the button).

    Best regards,
    Adam

  • Arthur

    Hi Adam,

    Sorry for the delay in getting back to you, had a few other things to deal with post launch (including nice things like communicating with my list. :slight_smile: )

    That seems to work well, yes - no signup info for logged in members. I was thinking I could have left the premium membership there if not signed up, but I can advertise that separately, let's not complicate things.

    So re the CSS, all I really want is for it to use the same (or similar) button styling to the other buttons on the site - bigger than the current ones, and blue so they stand out; the other elements are square so they should probably be square too.

    The protection message could be in bold and blue/dark red.

    The membership title and description is ok as is, since it picks up the bold.

    The other thing is on the registration form, I'd like the fields to be bigger (they are really tiny!)

    Thanks!
    Arthur

  • Adam Czajczyk

    Hi Arthur

    I think this CSS should work for the "signup block" (signup button and protection message):

    .single .ms-protection-msg {
    	font-weight:bold;
    	padding:20px 0;
    	/* color below is blue so edit the value if you wish to change it */
    	/* it's a color of protection message */
    	color:#0000FF;
    }
    
    .single .ms-membership-form .ms-membership-details-wrapper {
    	margin-bottom:25px;
    }
    
    .single .ms-membership-form button.ms-signup-button {
    	padding:10px 20px;
    	-webkit-border-radius: 0;
      border-radius: 0;
    	border:0;
    	background:#2E9ED5;
    	font-weight:bold;
    	color:#FFF;
    }
    .single .ms-membership-form button.ms-signup-button:hover {
    	background: #0389CD;
    }

    This CSS should take care of a signup form that shows up after you click "Signup" button from a protected post:

    #ms-shortcode-register-user-form input:not([type='checkbox']),
    #ms-shortcode-register-user-form textarea,
    #ms-shortcode-register-user-form select
    
    {
    	border:1px solid #ccc;
    	padding:8px 15px;
    	font-size:15px;
    	background:#efefef;
    	width:50%;
    }
    
    #ms-shortcode-register-user-form .ms-extra-fields p {
    	width:100%;
    	padding-top:10px;
    }
    
    #ms-shortcode-register-user-form .ms-extra-fields p input[type='checkbox'] {
    	float:left;
      display:block;
    	margin-right:20px;
    	margin-left:10px;
    }
    
    #ms-shortcode-register-user-form button.wpmui-submit {
    	padding:10px 20px;
    	-webkit-border-radius: 0;
      border-radius: 0;
    	border:0;
    	background:#2E9ED5;
    	font-weight:bold;
    	color:#FFF;
    }
    
    #ms-shortcode-register-user-form button.wpmui-submit:hover {
    		background: #0389CD;
    }

    In order to apply it to your site, copy and paste these codes (order doesn't matter) into the "Appearance -> Customize -> Additional CSS" or to some "custom css" option of your theme options (if there is such setting).

    Best regards,
    Adam

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.