Snippet: Code im Header, Body und Footer einfügen

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.

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.

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

WPCodeBox: Code in head and body
WPCodeBox: Code in head and body

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

functions.php: Code in Head und Body
functions.php: Code in Head and Body

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.

Leave a Reply

Your email address will not be published. Required fields are marked *