Theme & Front End PHP Beginner

Add Code to the Head and Footer

Add verification tags, tracking scripts or chat widgets to every page, in the head or just before the closing body tag.

Tested on WordPress 7.1 PHP 8.3 No plugin needed Updated
functions.php 13 lines
// Inside <head>: verification meta tags, preconnects, small inline styles.
add_action( 'wp_head', function (): void {
	?>
	<meta name="google-site-verification" content="your-verification-code">
	<?php
}, 1 );

// Before </body>: analytics, chat widgets and other scripts that can load last.
add_action( 'wp_footer', function (): void {
	?>
	<!-- Paste the script your service gave you here. -->
	<?php
}, 100 );

Where to add this

Recommended A code snippets plugin Survives theme changes and is easy to switch off.
Child theme At the end of your child theme's functions.php file.
Must-use plugin As a .php file in wp-content/mu-plugins, starting with <?php.

Always add PHP to a staging site first, or have FTP or file manager access ready, so you can undo it if something goes wrong.

Search Console verification, analytics tags and chat widgets all come with a “paste this into your site” snippet. Adding it with WordPress hooks keeps it in place through theme updates and makes it easy to remove later.

How it works

WordPress prints the wp_head action inside &lt;head&gt; and wp_footer just before &lt;/body&gt;. Closing the PHP tag inside each function lets you paste HTML exactly as your service provides it.

Priority 1 puts the head code near the top, which verification tools expect. Priority 100 puts footer code after most other scripts.

Things to watch

  • Only paste code from services you trust: it runs on every page.
  • Tracking scripts may need consent under privacy laws such as the GDPR. Load them through your cookie consent tool if they set cookies.
  • Page builders like Bricks have their own header and footer code settings, which do the same thing.

How to test it

View your page source. Your meta tag should appear near the top of the head, and footer code just before &lt;/body&gt;.

This website uses cookies to enhance your browsing experience and ensure the site functions properly. By continuing to use this site, you acknowledge and accept our use of cookies.

Accept All Accept Required Only