Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save ipokkel/a17a4c946712b51e103f8668e0213ffa to your computer and use it in GitHub Desktop.
Save ipokkel/a17a4c946712b51e103f8668e0213ffa to your computer and use it in GitHub Desktop.
PMPro Customizations to allow non-members to view specified restricted posts if they are less than 30 days old.
<?php
/**
* This recipe allows non-members to view specific restricted posts
* if they are less than 30 days old.
*
* To set which posts to unlock set the post id's in the array of
* the $posts_to_unlock variable
* Change the '-30 Days' below if you'd like to allow access for longer or shorter.
*
* You can add this recipe to your site by creating a custom plugin
* or using the Code Snippets plugin available for free in the WordPress repository.
* Read this companion article for step-by-step directions on either method.
* https://www.paidmembershipspro.com/create-a-plugin-for-pmpro-customizations/
*/
function open_specified_new_posts_to_non_members( $hasaccess, $thepost, $theuser, $post_membership_levels ) {
// Set your post id's to unlock here
$posts_to_unlock = array( 2, 35, 155 ); // array elements must be integers and not strings
global $wpdb;
//if PMPro says true already, return true
if ( $hasaccess ) {
return $hasaccess;
}
//figure out dates to check
$thepost_id = $thepost->ID;
$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 && in_array( $thepost_id, $posts_to_unlock, true ) ) {
$hasaccess = true;
}
return $hasaccess;
}
add_filter( 'pmpro_has_membership_access_filter', 'open_specified_new_posts_to_non_members', 10, 4 );
@laurenhagan0306
Copy link

This recipe is included in the blog post on "Lock or Unlock Posts Based on Age and Post Date" at Paid Memberships Pro here: https://www.paidmembershipspro.com/lock-unlock-posts-based-age-post-date/

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