Snippet: Insert Code in Header, Body and Footer
With these code snippets you can insert any code into the header, body or footer of your WordPress blog.
WordPress code snippets offer a great way to insert code into the header, body and footer of a WordPress website. The advantage is that no theme files need to be changed, which makes maintenance much easier.
Usable for:
- Tracking codes
- Javascript
- CSS
- CSS libraries
Afterwards you will find four different code examples and two variants, how you can insert the codes.
Attention: Make sure to make a backup before! The smallest mistakes can destroy your website.
Snippet for Header, Body and Footer
This is only the basic framework of how to insert code into the three areas of your WordPress website. You can get the codes themselves from the provider.
Replace the code comments () with your snippet.
The code
Header
<?php
add_action( 'wp_head', function() { ?>
<?php } );
Body
<?php
add_action( 'wp_body_open', function() { ?>
<?php } );
Footer
<?php
add_action( 'wp_footer', function() { ?>
<?php } );
Header and Body (Google Tag Manager):
<?php
add_action('wp_head', function() { ?>
<?php } );
add_action('wp_body_open', function() { ?>
<?php } );
The code snippet briefly explained:
The examples use a WordPress hook to signal exactly where to insert the snippet:
wp_head
wp_body_open
wp_footer
Alternative downloads of the code:
Use a snippet plugin
The easiest way to insert and activate small snippets is to use a code snippet plugin. You can use either Code Snippets or WPCodeBox.
Insert into the functions.php
If you have been using WordPress for a while, you may prefer to manage your snippets in the functions.php
file of your child theme.
You can do this directly in WordPress under Design > Theme File Editor > functions.php, or via FTP with your choice’s text editor.
Conclusion
If you do more with WordPress, eventually, you will need to add external code to your site. This is mostly about the position in the head
, body
or footer
.
For this, I have prepared three ready-made codes for you, which you can use directly on your website.
Since the question for an example for the Google Tag Manager comes up often, I have attached a code snippet for it.