Created
May 25, 2020 20:59
-
-
Save ipokkel/08e96809a57b94a34e49bc599212ad68 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 a specified amount of days old for that specific post.
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 recipe allows non-members to view specific restricted posts | |
| * if they are less than the amount of days specified per post old. | |
| * | |
| * To set which posts to unlock and their individual period (days) | |
| * set the post id's as the array key and the period as the | |
| * value of the array key of the $posts_to_unlock variable. | |
| * | |
| * 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_per_date_to_non_members( $hasaccess, $thepost, $theuser, $post_membership_levels ) { | |
| // Set your post id's and the unlock period here. | |
| $posts_to_unlock = array( | |
| 2 => '-30 Days', // post ID 2, less than 30 days old | |
| 35 => '-45 Days', // post ID 35, less than 45 days old | |
| 155 => '-14 Days', // post ID 155, less than 14 days old | |
| ); | |
| global $wpdb; | |
| //if PMPro says true already, return true | |
| if ( $hasaccess ) { | |
| return $hasaccess; | |
| } | |
| //figure out dates to check | |
| $thepost_id = $thepost->ID; | |
| $cutoff = strtotime( $posts_to_unlock[ $thepost_id ], current_time( 'timestamp' ) ); | |
| $published = strtotime( $thepost->post_date, current_time( 'timestamp' ) ); | |
| //if published after the cuttoff, then allow access for now | |
| if ( $published > $cutoff && array_key_exists( $thepost_id, $posts_to_unlock ) ) { | |
| $hasaccess = true; | |
| } | |
| return $hasaccess; | |
| } | |
| add_filter( 'pmpro_has_membership_access_filter', 'open_specified_new_posts_per_date_to_non_members', 10, 4 ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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/