Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save pbrocks/ff2a8ffcd78d651751c544f9089c697d to your computer and use it in GitHub Desktop.
Save pbrocks/ff2a8ffcd78d651751c544f9089c697d to your computer and use it in GitHub Desktop.
PMPro Customizations to allow non-members to view restricted posts if they are less than 30 days old.
<?php // do not include this line
/**
* Allow non-members to view restricted posts if they are less than 30 days old.
*
* Add this code to a custom plugin.
*
* Change the '-30 Days' below if you'd like to allow access for longer or shorter.
*/
/**
* [open_new_posts_to_non_members description]
*
* @param [type] $hasaccess [description]
* @param [type] $thepost [description]
* @param [type] $theuser [description]
* @param [type] $post_membership_levels [description]
* @return [type] [description]
*/
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;
}
// 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', 10, 4 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment