How to add a link to WordPress footer and header

26th June 2018

WordPress Website Maintenance Service UK WordPress

Are you struggling to figure out how to add a link or code to the header or footer of your WordPress website? There are several ways you can do this, some more complicated than others from plugins that help to child themes and editing WordPress theme code.

In this post, we will show how to add code or links to the header and footer of your WordPress website theme by using a plugin, child theme and with the functions.php file.

For those that instantly panic at the very sight of any code, this is probably the option you want to choose.

The good thing about using a plugin to do this is that whatever code or links you add via the plugin will stay intact even if you changed the theme as they are stored in the plugin.

We would recommend the very popular Head, Footer and Post Injections to do this.

Head, Footer and Post Injections

1. Install the plugin on your website

To do this login to your WordPress admin go to plugins and click the Add New button at the top of the page. In the search box type Head, Footer and Post Injections. Once you see the plugin in the search results, click install and then activate.

2. Using the plugin

Now that the plugin is installed you should now see it listed under the settings tab in the left admin menu of your site. Go to settings then click header and footer.

3. Adding your code

Now you are in the window to add your code. You will see a text box that says header or head and one that says before the end of page or footer. Those are the two boxes you want to fill in and then click save.

Childify Me WordPress Plugin

As mentioned there are a few ways you can achieve this. We will now look at doing it with a child theme.

This is a little more involved but will save the overhead of having to add an extra plugin. Having too many plugins can affect your WordPress loading speed after all.

Rather than learning how to create a child theme from scratch, we are going to cheat a little bit here!

 

1. Install a Child Theme Creator Plugin

This is a much easier way of creating a child theme. Install the plugin called Childify Me (we will remove this plugin again once the child theme is created)

2. Creating a WordPress Child Theme

Now that you have the plugin installed and activated, go to Appearance >> Customize in your WordPress admin menu. When that page loads scroll right to the bottom of the customiser page, and you should see create a child theme button. Click that, give your child theme a name and then click publish and activate. You can now delete the Childify Me plugin, your newly created child theme will still remain.

3. Copying parent theme files to a child theme

The next thing to do is copy over the parent theme (original them) header and footer files to your child theme. You can do this in many ways; some will choose to use an FTP client others might use something like Cpanel file manager. Whichever way you do it you need to copy those two files to your child theme.

Now you have those files copied over you can start editing them (the child them ones that is!). Basically, those files in the child theme now overwrite the original theme files. So if you add code or a link in those, it will show on your site as long as you have the child theme activated. This avoids losing your edits if the main parent theme gets updated. All you need to do now is add your code or links in those two files and save.

WordPress functions.php file

Although for some this might be a little daunting it’s, in my opinion, the quickest and easiest way to achieve this. We will simply be adding some code to the themes functions.php file. This can be in wither your parent (original theme) or child theme. Not though that if you don’t have a child theme, the edits may be lost when the theme is updated. That said you can just save this little snippet of code and pop it back.

As I said, this is probably the quickest way to do this. Simply copy the code below and add it to your themes functions.php file.

To add code to the header of your site use this code.

/* write anything here that reminds you what this code is for */
add_action('wp_head', 'your_function_name');
function your_function_name(){
?>
ADD THE CODE YOU WANT TO SHOW IN THE HEADER HERE
<?php
};

Don’t forget the change in the above code:

  • write anything here that reminds you what this code is for
  • the two instances of your_function_name, change that to whatever you want like header_analytics_code or something to that effect
  • ADD THE CODE YOU WANT TO SHOW IN THE HEADER HERE
/* write anything here that reminds you what this code is for */
add_action('wp_footer', 'your_function_name');
function your_function_name(){
?>
ADD THE CODE YOU WANT TO SHOW IN THE HEADER HERE
<?php
};

Don’t forget the change in the above code:

  • write anything here that reminds you what this code is for
  • the two instances of your_function_name, change that to whatever you want like header_analytics_code or something to that effect
  • ADD THE CODE YOU WANT TO SHOW IN THE HEADER HERE

That’s it you should now have added code to the header and footer of WordPress or your theme. I hope you found this article of use. If you did, please consider sharing or commenting.

Gavin Pedley

Gavin Pedley

Gavin is the guy behind the award-winning ThriveWP. He has over 18 years of experience creating, developing, hosting and managing WordPress websites.

Gavin regularly shares his expertise via the ThriveWP blog and Youtube channel, where he creates informative and helpful WordPress tutorial videos.

Connect with Gavin on FacebookLinkedin or Twitter.

Share this article

Subscribe to receive articles right in your inbox

Get Your Free Guide On Keeping Your WordPress Website Safe

Subscribe to learn how to keep your WordPress website safe, starting with this free guide. Unsubscribe with one click at any time.

We hate SPAM and promise to keep your email address safe. Here’s our privacy policy.

SEND ME MY FREE EBOOKS!​

Three amazing products that will enhance your website performance, ranking and maximise your income! Our eBook offer includes three eBooks in one bundle.

We hate SPAM and promise to keep your email address safe. Here’s our privacy policy.