How do you add Google Analytics and a Search Console verification code to a WordPress block theme?
In short: In Wiozen Theme (since 1.2.0) you paste these codes into the "Site codes" card on the Settings tab under Appearance › Wiozen Theme. The Google Analytics tag and Search Console's HTML tag go into the "In the page head" box; as soon as you click "Save settings" the code appears in the <head> of every page. You don't edit a theme file or install a separate code plugin, and theme updates don't delete the code.
Why doesn't a block theme have header.php?
In classic themes you pasted these codes into header.php, just before the </head> tag. In a block theme the header is built from blocks in the Site Editor, and there's no file where you can edit the page's <head>. Code you write by hand into a theme file or functions.php is also deleted by the first theme update.
So in block themes code is added either with a header/footer code plugin or through the theme's own setting. Wiozen Theme does the second with the Site codes card. The code is stored in the database as a theme setting and isn't written into theme files.
Adding the code step by step
- Copy the code from its source. In Google Analytics, open your stream under Admin › Data streams and take the whole
gtag.jssnippet from the tagging instructions. In Search Console, choose "HTML tag" as the verification method when you add a property and copy the<meta name="google-site-verification" …>line. - In WordPress admin, open Appearance › Wiozen Theme and go to the Settings tab.
- In the "Site codes" card, paste the codes one under the other into the "In the page head" box.
- Click "Save settings" at the bottom of the page.
- Go back to Search Console and click "Verify". In Analytics, open the Realtime report and visit the site in another browser.
The theme prints the code as it is; it doesn't change, combine or defer it. Code in the head box is attached to the wp_head hook and code in the second box to wp_footer, both late in the order of that hook's output. Each box holds up to 20,000 characters.
These panel tabs open on a site connected to a Wiozen account with at least $5 in total top-ups.
What is the "At the end of the page" box for?
Code in the second box is added just before the </body> tag. Code that doesn't need to load before the page shows goes there: a live chat bubble, end-of-page scripts from some ad networks, or code a service tells you to add "before the closing body tag". The Google Analytics and Search Console tags belong in the head box; if the Search Console tag is found outside <head>, verification fails.
AdSense's auto ads code also goes into the head box. Placing an ad unit in a specific spot is a separate job; see the AdSense code guide.
Why are the boxes greyed out, and who can save them?
Only users with the unfiltered HTML capability (unfiltered_html) can change site codes. On a single WordPress site, administrators have it; on a multisite network only the network (super) admin does. If your host put DISALLOW_UNFILTERED_HTML in wp-config.php, administrators lose it too.
Without the capability the boxes are grey and the note "Only administrators who may post unfiltered HTML can change site codes." appears below them. Saving the other settings on the Settings tab then leaves any existing codes as they are; they aren't deleted.
The code runs on every page of the site. Paste only code that Google, Microsoft or another service you trust gave you; a script from a forum or a stranger runs in your visitors' browsers.
How do you check the code is on the page?
Open the site in a browser window where you're not logged in, right-click, choose "View page source" and search for google-site-verification or gtag. If the code is there, the theme has done its part.
- If you don't see the code, clear the cache of your caching plugin and your hosting panel first; an old copy of the page may not contain it.
- Don't rely on the preview: the theme doesn't print these codes in the Customizer preview, and preview visits aren't recorded in Analytics.
- If you add the same Analytics code here and in a plugin (Site Kit, for example), every visit is counted twice. Keep the code in one place.
What happens to the codes if you switch themes?
The codes are a Wiozen Theme setting and are printed only while Wiozen Theme is active. The moment you switch to another theme, Analytics stops collecting data and the Search Console tag leaves the page. Google re-checks verification from time to time, so if the tag is gone for good the property can lose its verification. Before switching themes, move the codes to the new theme or to a code plugin. If you come back to Wiozen Theme, the saved codes are printed again.
If you verified Search Console with a DNS record, that record doesn't depend on the theme; switching themes doesn't affect it.
Frequently asked questions
Can I add the Google Tag Manager code here too?
Yes. Paste Tag Manager's first snippet into the "In the page head" box. The second snippet (<noscript>) is meant to go right after the opening <body> tag; Wiozen Theme has no box that adds code there. That snippet only serves visitors with JavaScript turned off; Tag Manager works without it.
Can't I verify Search Console by uploading an HTML file instead?
You can. The file method doesn't depend on the theme; you upload the file to the site's root folder with your hosting panel's file manager. The HTML tag method is done from the panel without opening the file manager.
Does a theme update delete the codes?
No. The codes are stored in the database as a theme setting, not in theme files. An update replaces the files and leaves the settings alone.
How do I move the codes to another site?
With the "Settings backup" card on the Customize tab. The backup file includes the site codes. Details are in the settings backup guide.
To connect Analytics and Search Console without a code plugin, download Wiozen Theme for free. For ad placements, see the AdSense code guide.