To add code to your header, use this code snippet:
/* Describe what the code snippet does so you can remember later on */
add_action('wp_head', 'your_function_name');
function your_function_name(){
?>
PASTE HEADER CODE HERE
<?php
};
To add code to your footer, use this code snippet:
/* Describe what the code snippet does so you can remember later on */
add_action('wp_footer', 'your_function_name');
function your_function_name(){
?>
PASTE FOOTER CODE HERE
<?php
};
For each snippet, make sure to change:
The comment description (helpful when you need to remember what a code snippet does a year later)
The your_function_name placeholder (both instances)
The PASTE X CODE HERE placeholder
Step 2: Add Code Snippets to functions.php File in Child Theme
Once you have the relevant code snippet(s) ready, you need to add them to the functions.php file of your child theme. You can either edit this file by connecting to your site via FTP. Or, you can go to Appearance → Editor and select the functions.php file. Then, paste your code at the end of the file:
Add code to functions.php file
Make sure to save your changes and you’re done!
BONUS: Add Code to Header or Footer For Only Specific Pages
If you want more control over where your header or footer code snippets show up, you can use if statements to only add the code to specific pages on your WordPress site.
For example, to only add code snippets to the header or footer of your homepage, you could use:
/* Describe what the code snippet does so you can remember later on */
add_action('wp_head', 'your_function_name');
function your_function_name(){
if(is_front_page()) { ?>
PASTE HEADER CODE HERE
<?php }
};
Another option is to only add the code snippets tospecific posts or pages. To do that, you can use this code snippet:
/* Describe what the code snippet does so you can remember later on */
add_action('wp_head', 'your_function_name');
function your_function_name(){
if(is_single(73790)) { ?>
PASTE HEADER CODE HERE
<?php }
};
Make sure to replace the example number – 73790 – with the actual ID of the post or page you want to add the code snippets to.