Skip to content

Instantly share code, notes, and snippets.

@georgestephanis
Last active August 29, 2015 14:01
Show Gist options
  • Save georgestephanis/c552e07acd1d9fe86efd to your computer and use it in GitHub Desktop.
Save georgestephanis/c552e07acd1d9fe86efd to your computer and use it in GitHub Desktop.
<?php
add_action( 'template_redirect', 'my_stats_template_redirect_tweak', 2 );
function my_stats_template_redirect_tweak() {
global $stats_footer;
// If we don't have a $stats_footer global ... maybe Stats or Jetpack isn't active?
if ( empty( $stats_footer ) ) {
return;
}
// If the user has just hit a button to okay stats
if ( ! empty( $_POST['user_okayed_stats'] ) ) {
$time = time();
setcookie( 'user_okayed_stats', $time, $time + YEAR_IN_SECONDS, COOKIEPATH, COOKIE_DOMAIN, false, false );
$_COOKIE['user_okayed_stats'] = $time;
}
// If the user hasn't okayed stats, don't show stats, but offer a form instead:
if ( empty( $_COOKIE['user_okayed_stats'] ) ) {
$stats_footer = '';
add_action( 'wp_footer', 'my_stats_opt_in_form', 99 );
}
}
function my_stats_opt_in_form() {
?>
<form method="POST">
<input type="submit" name="user_okayed_stats" value="<?php esc_attr_e( 'Opt-In to Stats Tracking!' ); ?>" />
</form>
<?php
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment