Last active
July 3, 2023 17:32
-
-
Save kimcoleman/08c2d466ea370ae19bab983aeb0140cd to your computer and use it in GitHub Desktop.
Notification bar when post views are being tracked and restricted by the Limit Post Views Add On
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 | |
/** | |
* Notification bar when post views are being tracked and restricted by the Limit Post Views Add On | |
*/ | |
function add_notification_bar_for_limit_post_view() { | |
// check for past views and if we are on a single page. needs to check if the post is locked at all by default. | |
if ( ! empty( $_COOKIE['pmpro_lpv_count'] ) && is_single() ) { | |
global $current_user; | |
// Check cookie for views value. | |
$parts = explode( ';', sanitize_text_field( $_COOKIE['pmpro_lpv_count'] ) ); | |
$limitparts = explode( ',', $parts[0] ); | |
// Get the level limit for the current user. | |
if ( defined( 'PMPRO_LPV_LIMIT' ) && PMPRO_LPV_LIMIT > 0 ) { | |
$limit = intval( PMPRO_LPV_LIMIT ); | |
$remaining_views = $limit - ($limitparts[1] + 1); | |
$f = new NumberFormatter( 'en', NumberFormatter::SPELLOUT ); | |
?> | |
<div style="background: #d4f1df; bottom: 0; font-size: 2.5rem; left: 0; padding: 1em; position: fixed; width: 100%; "> | |
You have <span style="color: #B00000;"><?php echo esc_html( $f->format( $remaining_views ) ); ?></span> | |
free article<?php if ( $remaining_views > 1 ) { echo 's'; } ?> | |
remaining. <a href="<?php echo wp_login_url( get_permalink() ); ?>" title="Log in">Log in</a> or <a href="<?php echo pmpro_url( 'levels' ); ?>" title="Subscribe now">Subscribe</a> now for unlimited online access. | |
</div> | |
<?php | |
} | |
} | |
} | |
add_action( 'wp_footer' , 'add_notification_bar_for_limit_post_view', 15 ); |
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 "Improve the user experience (and increase signups) when using the Limit Post Views Add On" at Paid Memberships Pro here: https://www.paidmembershipspro.com/improve-the-user-experience-and-increase-signups-when-using-the-limit-post-views-add-on/