The WordPress way to install Clarity is clunky
You want heatmaps and session recordings without paying Hotjar subscription prices. Microsoft Clarity is completely free but installing it still means editing a theme file or adding a plugin.
The canonical WordPress paths are: install an analytics plugin (comes with admin pages, updates, subscription nags), paste the snippet into functions.php (breaks when you switch themes), or set up Google Tag Manager as middleware (overkill for one pixel).
What most people do instead
How to add Microsoft Clarity to WordPress without a plugin
Clarity is a single script in the head. A must-use plugin on the wp_head hook handles it:
// wp-content/mu-plugins/clarity.php
add_action( 'wp_head', function () { ?>
<script type="text/javascript">
(function(c,l,a,r,i,t,y){
c[a]=c[a]||function(){(c[a].q=c[a].q||[]).push(arguments)};
t=l.createElement(r);t.async=1;t.src="https://www.clarity.ms/tag/"+i;
y=l.getElementsByTagName(r)[0];y.parentNode.insertBefore(t,y);
})(window, document, "clarity", "script", "XXXXXXXXXX");
</script>
<?php } ); Replace XXXXXXXXXX with your Clarity project ID. The familiar trade-offs apply:
- A
functions.phpversion disappears on a theme switch (usemu-plugins). - The project ID is hardcoded in the file.
- It records logged-in admin sessions too, unless you exclude them.
- No consent gating, which matters more here because Clarity records sessions.
The command does it in one step, consent-aware, with no file to maintain.
A better way: one command, one ID, tracking live
Run enable clarity with your Project ID. The command auto-registers as a startup command so the tracking snippet outputs on every page load. No theme edit, no plugin, no GTM middleware.
Shown in advanced mode, where commands start with tp. In easy mode you type the same command without the tp prefix.
Consent-aware flag included. Pass -consent_aware=true and the pixel only fires when your cookie-consent plugin's gate has been accepted. Works with any standard WP consent plugin that sets a cookie flag.
How it works
The command hooks wp_head with the Microsoft-provided tracking snippet, then auto-registers itself in Startup Commands so the tracking stays active across requests. Re-running with a different ID updates the existing entry. Disable from Startup Commands to remove.
tp enable clarity -cid=abcdefghij| Parameter | Value |
|---|---|
-cid (required) | Project ID (format: abcdefghij) |
-consent_aware | true to only fire after cookie consent (checks standard consent cookie) |
| Scope | Auto-registered as startup command, runs on every frontend page load |
| Where to find the ID | Clarity dashboard → Settings → Overview |
| Can be used in |
Real example
A client asks why users abandon their checkout. Google Analytics shows the funnel but not the why. Clarity's session recordings would answer, but you don't want to commit to Hotjar's pricing just to investigate. Run tp enable clarity -cid=abc123def, let it collect for a week, then watch recordings to spot where users get stuck.
Goes further with TrueCommander
Frequently asked questions
wp_head hook, as shown above. The trade-offs are a hardcoded project ID, no consent gating, and recording that includes admins. TrueCommander does the same in one command, consent-aware.