Home » Themes & functions.php » Header or Footer Changes in WordPress: functions.php Guide

Header or Footer Changes in WordPress: functions.php Guide

Harnessing the power of WordPress, you’ll often face scenarios requiring header or footer changes. Fortunately, WordPress child themes provide an ideal playground to implement these tweaks without disrupting the parent theme’s structure. However, suppose you make changes directly to your child theme’s “header.php” and “footer.php” files. In that case, you might not receive the latest updates if the parent theme’s respective files are updated.

The current article is "6.9. Header/Footer Changes" of our Complete SEO Guide Box.
Previous Article: 6.8. Create Child Theme. Next Article: 6.10. Adding Yoast Breadcrumbs

Understanding Child Themes and Their Functionality

The reason why child themes are recommended for implementing header or footer changes is simple: safeguarding your modifications. If changes are made directly to the parent theme, an update to the parent theme can erase them all. A child theme prevents this by containing all your adjustments separately.

Rather than diving directly into “header.php” and “footer.php,” there’s a better, more flexible route for your header or footer changes. That is through the “functions.php” file in your child theme. This file acts similarly to a plugin, allowing you to add features and functionality to your WordPress site while keeping everything compatible with future parent theme updates.

First, you need to make sure you created a Child theme. Follow our Guide to Create a Child Theme in WordPress.

You may be wondering how to use “functions.php” to implement header or footer changes. In WordPress, there are tools called hooks and filters. These let you insert your code into the WordPress core, themes, or plugins without overwriting any existing code.

Here’s a basic example of how you might add custom text to your header:

function custom_header_text() {
    echo '<p>This is my custom header text!</p>';
}
add_action('wp_head', 'custom_header_text');

In this example, the function “custom_header_text” outputs a simple paragraph of text. Using “add_action,” we attach this function to the “wp_head” action. This action is usually triggered in the <head> section of your site, generally located in the “header.php” file.

A similar method can be used for footer changes:

function custom_footer_text() {
    echo '<p>This is my custom footer text!</p>';
}
add_action('wp_footer', 'custom_footer_text');

This code adds a custom text to your footer.

Note: another useful hook is “wp_body_open”, which adds a snippet right after the <body> tag.

A Word of Caution

While these are rudimentary examples, the “functions.php” file has vast potential. It can be used to add scripts, define new widget areas, or even create custom image sizes. However, it requires familiarity with PHP and WordPress conventions. Always keep a backup and test on a development site before making live changes.

Theme Hooks

While “wp_head” and “wp_footer” are native WordPress hooks for the header and the footer, there is a more granular approach to using Theme hooks. Each theme can have its own hooks. Here is the list of Astra Theme hooks and their placement over the page.

Conclusion

Using the “functions.php” file for header or footer changes in your WordPress child theme preserves your alterations. It ensures you’re up-to-date with parent theme updates. It’s an indispensable tool for any aspiring WordPress developer or website owner.

The current article is "6.9. Header/Footer Changes" of our Complete SEO Guide Box.
Previous Article: 6.8. Create Child Theme. Next Article: 6.10. Adding Yoast Breadcrumbs

 

If you find any mistakes or have ideas for improvement, please follow the email on the Contact page.

Leave a Comment

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.