Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MaryOJob/60ba5fb4f5c15f098763f542bb3a1301 to your computer and use it in GitHub Desktop.
Save MaryOJob/60ba5fb4f5c15f098763f542bb3a1301 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
<?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