Skip to content

Instantly share code, notes, and snippets.

@benhuson
Created September 8, 2015 08:20
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 benhuson/3cd0d6c76a9b50a1bfb6 to your computer and use it in GitHub Desktop.
Save benhuson/3cd0d6c76a9b50a1bfb6 to your computer and use it in GitHub Desktop.
Google Tag Manager Patch - Only output tag once. https://wordpress.org/plugins/google-tag-manager/
Index: trunk/google-tag-manager.php
===================================================================
--- trunk/google-tag-manager.php (revision 1182053)
+++ trunk/google-tag-manager.php (working copy)
@@ -11,6 +11,8 @@
class google_tag_manager {
+ public static $output_once = false;
+
public static function go() {
add_filter( 'admin_init', array( __CLASS__, 'register_fields' ) );
add_action( 'wp_footer', array( __CLASS__, 'print_tag' ) );
@@ -28,7 +30,7 @@
<?php
}
public static function print_tag() {
- if( ! $id = get_option( 'google_tag_manager_id', '' ) ) return;
+ if( self::$output_once || ! $id = get_option( 'google_tag_manager_id', '' ) ) return;
?>
<!-- Google Tag Manager -->
<noscript><iframe src="//www.googletagmanager.com/ns.html?id=<?php echo $id; ?>"
@@ -40,6 +42,7 @@
})(window,document,'script','dataLayer','<?php echo $id; ?>');</script>
<!-- End Google Tag Manager -->
<?php
+ self::$output_once = true;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment