Skip to content

Instantly share code, notes, and snippets.

@JarrydLong
Last active November 20, 2025 12:17
Show Gist options
  • Select an option

  • Save JarrydLong/3ac5d3c30252c7356e69ffa4fae5a7f5 to your computer and use it in GitHub Desktop.

Select an option

Save JarrydLong/3ac5d3c30252c7356e69ffa4fae5a7f5 to your computer and use it in GitHub Desktop.
<?php //do not copy
// Check whether the user has granted Cookiebot consent for cookies beyond necessary.
function my_pmpro_determine_if_set_cookie() {
if ( ! isset( $_COOKIE['CookieConsent'] ) ) {
// User hasn't made a choice — only strictly necessary cookies allowed.
return false;
}
$valid_php_json = preg_replace('/\s*:\s*([a-zA-Z0-9_]+?)([}\[,])/', ':"$1"$2', preg_replace('/([{\[,])\s*([a-zA-Z0-9_]+?):/', '$1"$2":', str_replace("'", '"',stripslashes($_COOKIE["CookieConsent"]))));
$CookieConsent = json_decode( $valid_php_json );
// Handle malformed or empty cookies.
if ( ! $CookieConsent ) {
return false;
}
// Allow cookies if user accepted any of these categories.
return (
( isset( $CookieConsent->preferences ) && $CookieConsent->preferences !== 'false' ) ||
( isset( $CookieConsent->statistics ) && $CookieConsent->statistics !== 'false' ) ||
( isset( $CookieConsent->marketing ) && $CookieConsent->marketing !== 'false' )
);
}
/**
* Register PMPro tracking hooks only if Cookiebot consent allows it.
*/
function my_pmpro_remove_tracking() {
remove_action( 'wp', 'pmpro_report_login_wp_visits' ); // Visits
remove_action( 'wp_head', 'pmpro_report_login_wp_views' ); // Views
remove_action( 'wp_login', 'pmpro_report_login_wp_login', 10, 2 ); // Logins
}
add_action( 'init', 'my_pmpro_remove_tracking', 1 );
function my_pmpro_register_tracking_hooks() {
if ( my_pmpro_determine_if_set_cookie() ) {
add_action( 'wp', 'pmpro_report_login_wp_visits' ); // Visits
add_action( 'wp_head', 'pmpro_report_login_wp_views' ); // Views
add_action( 'wp_login', 'pmpro_report_login_wp_login', 10, 2 ); // Logins
}
}
add_action( 'init', 'my_pmpro_register_tracking_hooks', 99 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment