GTM4WP for developers
You can extend GTM4WP from your own code. This section is for theme and plugin developers who want to change what the plugin sends to Google Tag Manager, and for site managers who run several environments or keep the container settings in code.
This page describes GTM4WP 2.1.0.
Ways to extend GTM4WP
- Actions and filters change the data layer, the ecommerce product data and other parts of the output. The actions and filters page lists every hook.
- Template functions, described below, let a theme place the container code or queue its own data layer event.
- Constants in wp-config.php set the container ID and the environment parameters in code, as the guide to hard-coding GTM settings in wp-config.php explains.
- A site with staging or development copies can load a different Google Tag Manager environment on each of them, as the guide to GTM environments explains.
- A plugin can add a section of its own to the GTM4WP settings screen through the
gtm4wp_register_modulesaction, described on the actions and filters page. - A module can add its own rows and status tests to the GTM4WP section of Site Health.
- AI assistants can read and explain the GTM4WP settings through the WordPress Abilities API, as the MCP page describes.
Template functions
Two functions are meant for theme and plugin code. Wrap each call in function_exists(), so that your code keeps working while GTM4WP is deactivated.
gtm4wp_the_gtm_tag() prints the noscript part of the container code. A theme that does not call wp_body_open places it right after the opening body tag, with the Container code placement option set to Manually coded after the opening body tag.
gtm4wp_datalayer_push() queues a data layer event. The plugin prints it in the page footer, after the container code, so call the function anywhere before WordPress prints the footer scripts. The event name becomes the event key of the push, and the keys of the array are added next to it:
<?php
if ( function_exists( 'gtm4wp_datalayer_push' ) ) {
gtm4wp_datalayer_push( 'newsletter_signup', array( 'formLocation' => 'footer' ) );
}
The function returns true when the event was queued, and false when an argument has the wrong type.
Code written for GTM4WP 1.x
Version 2.0 rewrote the plugin but kept most of its public interface: the hook names, the template functions, the wp-config.php constants and the gtm4wp-options database option still work. GTM4WP still fills the global variables $gtm4wp_options and $gtm4wp_datalayer_name for code that reads them, but writing to them no longer changes what the plugin does. The hooks that were removed are listed on the actions and filters page.
Source code and changelog
The source code is on GitHub, where you can also report a bug or request a feature. The changelog of every release is on the GTM4WP changelog page.
All developer guides
Google Tag Manager Environments
Set up GTM environments to separate your production environment from your developer and sandbox sites.
Hard code GTM parameters
Set your Google Tag Manager container and environment parameters in wp-config.php.
Site Health
Check the GTM4WP setup in WordPress Site Health and share it safely in a support request.
AI assistants and MCP
Let ChatGPT, Claude or another AI assistant read and explain your GTM4WP settings through MCP.

