Forked from strangerstudios/hide_old_posts_from_members.php
Last active
June 18, 2019 21:04
-
-
Save messica/f0f7b806a4cc17c5b9fd25a0d8707a12 to your computer and use it in GitHub Desktop.
Paid Memberships Pro: Hide Old Posts From New Members except specific categories.
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 | |
/** | |
* Hide old posts from new members, except for specific categories. | |
*/ | |
add_filter( 'pmpro_has_membership_access_filter', 'hide_old_posts_from_members', 10, 4 ); | |
function hide_old_posts_from_members( $hasaccess, $thepost, $theuser, $post_membership_levels ) { | |
global $wpdb; | |
// if PMPro says false already, return false | |
if ( ! $hasaccess ) { | |
return false; | |
} | |
// If the post has the category "tutorials", allow access. | |
if ( has_category( 'tutorials', $thepost ) ) { | |
return true; | |
} | |
// 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 ); | |
// no startdate? return false | |
if ( empty( $startdate ) ) { | |
return false; | |
} | |
// if the startdate is before the post date, return true | |
if ( $startdate < strtotime( $thepost->post_date ) ) { | |
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; | |
} | |
} | |
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