How to add a WordPress code snippet to your header and footer

H

Inserting a script or piece of code, known as a WordPress code snippet, into the header or footer of your website is a very common request.

A WordPress code snippet is a piece of PHP code that you add to your WordPress site to extend the functionality of your site. Many third-party services such as Google, Facebook, and Pinterest ask you to add tracking scripts to your header or footer. You may also need to add javascript and CSS links in your global header or footer, too.

If you’re running a WordPress website, there’s an easy solution for adding WordPress code snippets to your site. My go-to plugin for this task is the Code Snippets plugin by Code Snippets Pro.

The Code Snippets plugin

This plugin easily allows you to organize the customizations you make to your theme and add descriptions and apply and turn off your customizations.

To add scripts to the header of your WordPress site using a PHP function, you’ll use this code snippet:

add_action( 'wp_head', function () { ?>

	<!-- code to be shown on all pages of your website in the HEADER goes here -->

<?php } );

Copy and paste the snippet that you want to be shown within the header of your site to replace the comment shown above: <!– code to be shown on all pages of your website in the HEADER goes here –>”.

Use WordPress Snippets to add scripts or WordPress code snippets to the header of your website.
Use WordPress Snippets to add scripts to the header of your website.

Note that you can choose via the radio buttons below the code four options for visibility. In this case, you want to choose Only run on site front-end.

The Snippet plugin is a valuable tool for setting up a new Pinterest Business account, too, because this WordPress tool allows you to add your required Pinterest Header HTML tag to your website quickly and easily.

Add a script to your WordPress site footer with Snippets

To add a script to the footer of your website located right before the close of your body tag, you can also use the Snippets plugin.

add_action( 'wp_footer', function () { ?>

	<!-- code to be shown on all pages of your website in the FOOTER goes here -->
  
<?php } );
To add a script to the footer of your website, right before the close of your body tag, you can also use the Snippets plugin.

Copy and paste the snippet that you want to be shown within the header of your site to replace the comment shown above:  <!– code to be shown on all pages of your website in the FOOTER goes here –>.

If you found the Snippets WordPress plugin useful for adding scripts and functions to the header and footer of your website, let me know in the comments!

About the author

Kelly Barkhurst

Designer to Fullstack is my place to geek out and to share tech solutions from my day-to-day as a designer, programmer, and business owner (portfolio). I also write on Arts and Bricks, a parenting blog and decal shop that embraces my family’s love of Art and LEGO bricks!

Add comment

By Kelly Barkhurst

Recent Posts

Archives

Categories