Forked from strangerstudios/make_old_posts_free.php
Last active
May 28, 2020 10:56
-
-
Save ipokkel/15fc2fc382949b5e42473e4fe127bc70 to your computer and use it in GitHub Desktop.
Make any post older than 18 months available for free with Paid Memberships Pro.
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 older than the amount of days specified per post. | |
* | |
* 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 make_old_posts_free( $hasaccess, $post, $user, $post_membership_levels ) { | |
//if they already have access, let them at it | |
if ( $hasaccess ) { | |
return $hasaccess; | |
} | |
//only make posts of type post (i.e. not pages or other CPTs) free | |
if ( 'post' !== $post->post_type ) { | |
return $hasaccess; | |
} | |
// Set your post id's and the unlock period here. | |
$posts_to_unlock = array( | |
36 => '-30 Days', // post ID 21, 30 days old | |
35 => '-20 Days', // post ID 35, 20 days old | |
41 => '-10 Days', // post ID 155, 10 days old | |
); | |
//now check the publish date | |
$post_id = $post->ID; | |
$published = strtotime( $post->post_date, current_time( 'timestamp' ) ); | |
$open_date = strtotime( $posts_to_unlock[ $post_id ], current_time( 'timestamp' ) ); | |
if ( $published < $open_date && array_key_exists( $post_id, $posts_to_unlock ) ) { | |
$hasaccess = true; | |
} | |
return $hasaccess; | |
} | |
add_filter( 'pmpro_has_membership_access_filter', 'make_old_posts_free', 10, 4 ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment