Skip to content

Instantly share code, notes, and snippets.

@jack-arturo
Created December 10, 2024 15:43
Show Gist options
  • Save jack-arturo/97eda7080690cce511b538d6e2ec354b to your computer and use it in GitHub Desktop.
Save jack-arturo/97eda7080690cce511b538d6e2ec354b to your computer and use it in GitHub Desktop.
Applies a tag after any 5 pages on the site are viewed
<?php
/**
* Track total page views across the site and apply a tag with WP Fusion if the view count exceeds 5.
*/
function wpf_track_total_page_views() {
// Replace with the WP Fusion tag name you want to apply
$wp_fusion_tag = 'Example Tag Name';
$target_view_count = 5;
//
// Don't edit after here:
//
$wp_fusion_tag = wpf_get_tag_id( $wp_fusion_tag );
// Ensure user is logged in
if ( ! is_user_logged_in() ) {
return;
}
$user_id = get_current_user_id();
$meta_key = 'total_page_view_count';
$page_views = (int) get_user_meta( $user_id, $meta_key, true );
// Increment total page view count
++$page_views;
update_user_meta( $user_id, $meta_key, $page_views );
// Apply WP Fusion tag if view count exceeds the target and tag hasn't been applied yet
if ( $page_views > $target_view_count ) {
wp_fusion()->user->apply_tags( array( $wp_fusion_tag ), $user_id );
}
}
add_action( 'wp', 'wpf_track_total_page_views' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment