There are times when you as a website developer may want to introduce a code snippet in your WordPress theme and wondering how to go about doing that. Some snippets of code require you to add it to your header.php file and you find yourself scrambling for a solution that is efficient and can stand the test of time. We’re pretty sure that someone on the internet is yelling to add that snippet in the child theme and you nod your head meekly, agreeing with the advice…but still completely lost inside. “How do you copy header.php from the parent to the child theme?” - you shake your head in anguish - still searching for an answer and a way out.
In this post we’ll show how you can easily copy the header.php file from the parent theme to the child theme in WordPress. But before we get to that, let’s ask ourselves why we need to go through the exercise and the extra effort?
Historically speaking, in WordPress there was just one type of theme that included all the functionality required to display the content of your website. This theme is known as the parent theme. You could make any number of changes to this parent theme and the website would ultimately reflect those changes on the front-end for you. However, if you wanted to update the theme at any time (which you should for a variety of reasons), you’d stand to lose all the past changes you made to that theme. All the efforts you took to add customized styling to your parent theme will be gone as and when you update it. Which sucks, honestly.
So WordPress came up with an ingenious idea of a child theme that will inherit all the style, functionality and features of the parent theme. This parent-child relationship made it much easier for WordPress developers to add custom styling to a website without the fear of losing them. The parent theme is therefore left largely untouched as most of the customization and modification is done in the child theme. You therefore have the ability to update the parent theme without any risk of losing your added custom styles in the child theme.
Many of the modern day WordPress themes now come along with a child theme…or at least, they make it easy to create one. At WP Adventure, we work with Astra – one of the most popular WordPress themes there is on the market and it makes it incredibly easy to create a child theme.
Let’s see how we can copy any file from the parent theme to its child theme. For the sake of this post, we’ll take a step by step approach to copy the header.php file from the parent to the child theme.
Before proceeding with the exact steps, please ensure that you have uploaded your child theme and activated it in your WordPress. As you can see below, we have our child theme – WP Adventure – activated:
How to copy header.php from parent theme to child theme.
Since the vast majority of people use a cPanel based host, we will be using that for our demonstration purposes here.
1) Login to your cPanel account and click on File Manager
2) Make sure that you are in the root directory for your website. For this demo, we have our website in the public.html folder so we’ll open that.
3) Click on the wp-content folder and open it.
4) Click on the Themes folder and open it.
5) You’ll now see all the themes installed on your WordPress. We have 2 themes as seen below – the parent theme (Astra) and the child theme (WP Adventure)
6) If you want to see which files are inherited by the child theme, simply click on the child theme and they’ll get displayed. As you can see below, we have only the functions.php and style.css files in the child theme.
7) Now go back one step and click the parent theme folder to open it.
8) You’ll see the header.php file in it – which we want to copy over to the child theme.
9) So, right click header.php and click Copy.
10) You will now see a popup showing the location of the file in the parent theme folder (i.e. Astra in this case)
11) Simply change the folder name to the child theme folder and click Copy files. In our case here, the child theme folder name is child-theme-wpadventure, so we’ve changed it accordingly.
12) If you head back to the child theme folder, you should now see the header.php file copied over successfully in that location.
13) Now, go back to your WordPress backend, click Theme Editor under Appearance and select your child theme, you should now see the header.php file displayed here, as shown:
You can now open the header.php file and add the appropriate codes here as you wish.
That's about it. If you've followed all the steps outlined above, you would have successfully copied the header.php file from the parent theme to the child theme in WordPress.
This works not only for the header.php file but for any file that you'd like to copy over from the parent to the child theme. Hope this post helped you a bit!