Created
December 20, 2018 18:49
-
-
Save kimcoleman/d4dc3f4dacd445bd6a5b86abbff9c527 to your computer and use it in GitHub Desktop.
This code will create a content filter for pages to remove access to posts that were published before a member's join date, including a 15 day grace period.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
* This code will create a content filter for pages to remove access to posts that were published before a member's join date. | |
* It includes a 15 day "grace period" so that members can see posts published up to 15 days prior to their join date. | |
* | |
* The params passed are: | |
* $hasaccess - (bool) what PMPro thinks about whether the user has access | |
* $thepost - (post object) the post being checked, usually the current post | |
* $theuser - (user object) the user being checked, usually the current user | |
* $post_membership_levels - (array of levels) the levels this post requires (if any) | |
*/ | |
function studiotogo_hide_old_pages_from_members($hasaccess, $thepost, $theuser, $post_membership_levels) | |
{ | |
global $wpdb; | |
//if PMPro says false already, return false | |
if ( ! $hasaccess ) { | |
return false; | |
} | |
//if the post isn't a "page" just return. | |
if ( $thepost->post_type != 'page' ) { | |
return $hasaccess; | |
} | |
//if the post doesn't require membership, allow access | |
if ( ! $post_membership_levels ) { | |
return true; | |
} | |
//okay, this post requires membership. start by getting the user's startdate | |
$startdate = pmpro_getMemberStartdate( $theuser->ID ); | |
$grace_startdate = strtotime( '-15 days', $startdate ); | |
//no startdate? return false | |
if ( empty( $grace_startdate ) ) { | |
return false; | |
} | |
//if the startdate is before the post date, return true | |
if ( $grace_startdate < strtotime( $thepost->post_date, current_time( 'timestamp' ) ) ) { | |
return true; | |
} else { | |
//in this case we want to also tweak the message shown | |
add_filter( 'pmpro_non_member_text_filter', 'swap_old_posts_member_text' ); | |
return false; | |
} | |
} | |
add_filter( 'pmpro_has_membership_access_filter', 'studiotogo_hide_old_pages_from_members', 10, 4); | |
// Swap the noaccess message. | |
function swap_old_posts_member_text( $s ) { | |
$s = "This content was published before your membership started."; | |
return $s; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment