Skip to content

Instantly share code, notes, and snippets.

@andrewlimaza
Forked from strangerstudios/make_old_posts_free.php
Last active August 31, 2021 10:01
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save andrewlimaza/a5ec5a152251daf320caef6a7840e058 to your computer and use it in GitHub Desktop.
Save andrewlimaza/a5ec5a152251daf320caef6a7840e058 to your computer and use it in GitHub Desktop.
Make any post older than 18 months available for free with Paid Memberships Pro.
<?php
/**
* Allow non-members to view restricted posts if they are less than 30 days old.
* Change the '-30 Days' below if you'd like to allow access for longer or shorter.
* Adjust $categories array for the categories this should apply to.
* Add this code to your Customizations Plugin by following this guide - https://www.paidmembershipspro.com/create-a-plugin-for-pmpro-customizations/
*/
function open_new_posts_to_non_members($hasaccess, $thepost, $theuser, $post_membership_levels) {
global $wpdb;
//if PMPro says true already, return true
if ( $hasaccess ) {
return $hasaccess;
}
// Categories that this code should apply to. Categories not here will assume default functionality.
$categories = array( 'category-1', 'category-2' );
if ( ! in_category( $categories, $thepost ) ) {
return $hasaccess;
}
//figure out dates to check
$cutoff = strtotime( '-30 Days', current_time( 'timestamp' ) );
$published = strtotime( $thepost->post_date, current_time('timestamp') );
//if published after the cuttoff, then allow access for now
if ( $published > $cutoff ) {
$hasaccess = true;
}
return $hasaccess;
}
add_filter("pmpro_has_membership_access_filter", "open_new_posts_to_non_members", 20, 4);
@ravasheen
Copy link

I am looking to leave patreon and this is a HUGE step in making PMP acting like the early access feature in patreon. Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment