I’m creating a child theme for the first time and I had a few questions regarding code added to header.
In a non child theme there is certain code I add to my header.php file such as google analytics, google webmaster tools, buy sell ads, Facebook open graph, etc….
How do you do this in a child theme? Do you create a header.php file in your child theme? If so how is this done? Is it the same as the @import as I used on the css?
Thanks.
I would hook into the
wp_head
action. I would place this in a plugin so as to abstract it from your presentation layer. This allows for scalability and changing of themes. This also prevents any analytics collateral damage if a step is missed in migration from one theme to the next.Adding a Code Snippet
HTML
If you want to add a code snippet e.g. a meta tag etc to the
<head>
, then you should use thewp_head
action:There are also
admin_head
andwp_footer
Javascript & CSS
You could use the above code to also add Javascript, but instead custom JS should be enqueued as a JS file, or, included as inline JS via
wp_add_inline_script
attached to an existing enqueued JS handle.https://developer.wordpress.org/reference/functions/wp_add_inline_script/
This is also true of CSS, which has
wp_add_inline_style
https://developer.wordpress.org/reference/functions/wp_add_inline_style/Modifying the Sites Header
Note that once you do this, you will not see changes made in the parent theme when it updates for those files that you override and modify.
In A Block Theme
If your theme is a block theme then you can do this by going to the site editor and making changes in the Admin area.
In a Classic Theme
If your site is a classic PHP theme, then you can use a child theme.
To modify the header in a child theme, copy the file
header.php
from the parent theme into the child theme and then modify it. WordPress will see that you have aheader.php
in your child theme and use that instead of the parent themeheader.php
Any template files you put in your child theme will take priority over the same file in the parent theme when called by WordPress.
Anything that goes in the
<head>
tag should be done using something such as the function in Brians answer. If it’s theme specific, you can put it in a file calledfunctions.php
in your theme folder without any extra steps.Thanks to Brian Fegter. If this answer helps, please rate for Brian’s answer right here above.
This is a fully functional example of how to add things to the “header” by its own plugin. In this case, I am adding the properties of Facebook Open Graph for the Share and Like buttons.
Just create a PHP file with the name specified in “Plugin Script” at the beginning of the sample code, place it in a folder with the same name without the extension, obviously, and copy this folder to the destination “/ wp-content / plugins”.
Then within “WordPress”, refresh “Plugins” and you’ll see your new plugin installed. Just Activate it, and your pages will begin to contain the metadata of Open Graph Facebook and Twitter.
VERY IMPORTANT: The PHP file must be encoded in UTF-8 without BOM, and should have absolutely no character at the end. Must ensure this.
Anyone who is interested in the functionality of the plugin.
The title will be the concatenation of the name of the current page
and the site name.
If a custom field called “metadescription” exists, the plugin tries
to take the description from this field. Otherwise, take the
description from the excerpt.
As the image, the plugin tries to use the thumbnail of the featured
image on the page.