Download and test GTM4WP 2.0.0 beta2. Don’t wait for others to test it, they are waiting for you!

Skip to content
Facebook Twitter Github RSS
Google Tag Manager for WordPress
  • Home
  • FeaturesExpand
    • Basic features of GTM4WP

      You can use many post or page attribute in you Google Tag Manager setup even for custom post types and taxonomies.

      Basic data

      Page / post attributes

      Site search

      User data

      Multisite data

      3rd party data

      Browser attributes

      Device attributes

      Operating system attributes

      Weather conditions

  • SetupExpand
    • Setup advanced GTM4WP features

      Integrate your measurement with WooCommerce, setup Google Analytics 4, Google Ads remarketing or conversion tracking maximizing capabilities.

      WooCommerce related

      Google Analytics 4

      Google Ads – Dynamic Remarketing

      Google Ads – Conversion tracking

      Google Ads – Enhanced Conversions

      Other setup articles

      Cache Safe Data Layer

      Exclude WordPress admins

      Embedded media players

  • DevelopersExpand
    • Information for developers and WP managers

      There are several tools in GTM4WP to support your special needs and to adjust plugin behavior to your website or WooCommerce shop.

      Do you want to contribute with a bugfix or a new feature? Visit the GTM4WP’s GitHub page!

      For Developers

      Actions and filters

      WP themes + Proper ecommerce tracking

      For WP managers

      Setup GTM Environments

      Hard code GTM parameters

  • Blog
  • GDPR
Download
Google Tag Manager for WordPress
Home / GTM4WP For Developers / Hard code Google Tag Manager parameters in wp-config.php

Hard code Google Tag Manager parameters in wp-config.php

You can set a fixed Google Tag Manager container ID and environment parameters in wp-config.php using PHP constants. This gives you better workflows when dealing with different environments to develop and test updated versions of the whole site.

A constant overrides whatever is stored in the plugin settings. While it is defined, the value saved on the settings screen is ignored for that parameter, so the only way to change or remove it is to edit your wp-config.php file.

This page describes GTM4WP 2.0.0 and newer. Where the legacy 1.x versions of the plugin behaved differently, the older behavior is marked with a bold GTM4WP 1.x: lead-in.

Use the following PHP constants:

  • GTM4WP_HARDCODED_GTM_ID
  • GTM4WP_HARDCODED_GTM_ENV_PREVIEW
  • GTM4WP_HARDCODED_GTM_ENV_AUTH
  • How the two environment constants apply
  • What happens to an invalid value

GTM4WP_HARDCODED_GTM_ID

This has to be one or more valid Google Tag Manager IDs (GTM-XXXXX). If you want to load multiple containers, separate the list with commas. You must not use spaces around commas when listing the container codes. Every ID has to consist of GTM- followed by capital letters and digits.

Each hard coded ID keeps the settings of the container row that already has the same ID in your plugin settings, so its custom domain, custom path and environment parameters are preserved. An ID that appears in no row inherits the settings of the first row. If any ID in the list has the wrong format, the whole constant is ignored and the settings screen shows a warning that names it.

GTM4WP_HARDCODED_GTM_ENV_PREVIEW

This must include the value of the gtm_preview parameter from the container code of your Google Tag Manager environment, which looks like env-4. If you set this constant, also set GTM4WP_HARDCODED_GTM_ENV_AUTH in order for the environment to properly load.

To learn more about how to get this parameter from the container code, please check the GTM environment setup guide.

GTM4WP_HARDCODED_GTM_ENV_AUTH

This must include the value of the gtm_auth parameter from the container code of your Google Tag Manager environment. It may contain letters, numbers, hyphens and underscores. If you set this constant, also set GTM4WP_HARDCODED_GTM_ENV_PREVIEW in order for the environment to properly load.

To learn more about how to get this parameter from the container code, please check the GTM environment setup guide.

How the two environment constants apply

Unlike the environment parameters you enter on a single container row in the settings, these two constants are site wide. They replace the environment parameters of every container row at once.

Because a complete environment override belongs to one container, only the first container ID is loaded while both constants hold a value. Use the per row environment parameters on the settings screen instead when you want to load several containers and put each of them into a different environment.

Setting a constant to an empty string is treated as a deliberate instruction to clear the environment rather than as a mistake, so it loads the live version of the container.

What happens to an invalid value

All three constants are checked against the same rules the settings screen enforces. A value in the wrong format is ignored rather than written into the loader URL of your container, and the settings screen shows an error that names the constants you need to correct. The rest of your configuration keeps working while you fix it.

GTM4WP 1.x: only the container ID was validated. A malformed environment parameter was written into the loader URL as it was, with no warning, which could stop the container from loading at all.

© 2026 Google Tag Manager for WordPress - Privacy policy

  • WordPress.org plugin page
  • Plugin support
  • Plugin reviews
  • Home
  • Features
    • Basic features of GTM4WP

      You can use many post or page attribute in you Google Tag Manager setup even for custom post types and taxonomies.

      Basic data

      Page / post attributes

      Site search

      User data

      Multisite data

      3rd party data

      Browser attributes

      Device attributes

      Operating system attributes

      Weather conditions

  • Setup
    • Setup advanced GTM4WP features

      Integrate your measurement with WooCommerce, setup Google Analytics 4, Google Ads remarketing or conversion tracking maximizing capabilities.

      WooCommerce related

      Google Analytics 4

      Google Ads – Dynamic Remarketing

      Google Ads – Conversion tracking

      Google Ads – Enhanced Conversions

      Other setup articles

      Cache Safe Data Layer

      Exclude WordPress admins

      Embedded media players

  • Developers
    • Information for developers and WP managers

      There are several tools in GTM4WP to support your special needs and to adjust plugin behavior to your website or WooCommerce shop.

      Do you want to contribute with a bugfix or a new feature? Visit the GTM4WP’s GitHub page!

      For Developers

      Actions and filters

      WP themes + Proper ecommerce tracking

      For WP managers

      Setup GTM Environments

      Hard code GTM parameters

  • Blog
  • GDPR
Search