Here's something I've done for a client recently. It could probably be prettied up a bit, but had to be done quickly. Sorry about all the PHP tags, but I've obviously removed a bunch of stuff to strip this down, so it makes more sense in context. Depending up on your situation, some of this templating may have to be in slightly different locations. This project is only protecting the single-entry pages, so we just built it right in there.
Someplace global like in header.php, we define a constant:
- Code: Select all
define("PREVIEW_EXPIRY", "86400"); // 1 day = 86400 seconds
Then, around the top of single.php we figure out if the entry is older than our
time limit:
- Code: Select all
<?php
if(is_single()) {
$pubDate = get_the_date('c');
$pubUnix = strtotime($pubDate);
$now = time();
if(($now - $pubUnix) > PREVIEW_EXPIRY) {
$expired = true;
}
}
?>
And then wherever it's appropriate to start wrapping the stuff you want to protect:
- Code: Select all
<?php if (current_user_can("access_s2member_level1")){ ?>
[This gets shown to people with whatever access you're testing for in that if statement]
<?php } else { // These are our non-subscribers ?>
<?php // Then in here, we can say:
if($expired) {
[ This post is more than PREVIEW_EXPIRY old, so show limited content ]
} else {
[ This post is less than PREVIEW_EXPIRY old, so show full content temporarily ]
}
?>
<?php } ?>