What Needs to Go in It and What Doesn’t

In this tutorial, let’s talk about the header.php file, an essential file for any WordPress theme. I’ll show you a  header file example and give tips about what needs to go in it and what doesn’t.

Introduction

It’s important for you to know what exactly the header.php file should contain in a WordPress theme.

We have more than a logo and menu in this file—we also have the head tag and lots of other tags, like link, script, meta, and title.

In the course of this article, we’ll create a demo header.php file, and we’ll discuss each and every element in that file.

Remember that the header of your site is the content which is shown on all the pages of your site. For example, the logo and menu are shown on all the pages of your site, and thus, they should be included in the header.php file.

If an element is shown only on a specific page, then you should not include it the header.php file.

The Sample Code

In this section, we’ll build a demo header.php file. In the next section, we’ll go through each and every piece of code to understand what it does.

But before we implement the header, let’s paste the following code at the top of your wp-includes/functions.php file. Remember to modify the paths to your CSS and JavaScript files as needed.

Next, let’s have a look at the sample header.php file. You should create this file at wp-content/themes/theme-name/header.php. Of course, you need to adjust the theme-name part with the actual name of your theme.

In general, the header.php file must include several elements, such as:

  • doctype
  • <html> tag
  • meta tags
  • head tags
  • favicon, RSS, and pingback
  • title
  • scripts and styles added with the wp_enqueue_script and wp_enqueue_style functions

In the next section, we’ll understand what we’ve implemented so far to build our custom header.php file.

The functions.php File in Detail

Let’s begin with the functions.php file. We’ve added these lines in the file:

Remove Meta Generator Tag

The following line removes the meta generator tag because it displays the WordPress version number. It’s not wise to expose your WordPress version number publicly, which could make you vulnerable to version-related attacks. Thus, we’ve removed it.

Adding Your CSS

In this case, we’ve created the enqueue_styles function to add link tags to the header.php file. The official WordPress documentation says that the best  way to add stylesheets (.css) and scripts (.js) is with the wp_enqueue_script and wp_enqueue_style functions. You can learn more about this in our article, How to Include JavaScript and CSS in Your WordPress Themes and Plugins.

Firstly, we created a function called enqueue_styles, and then we’ve called the add_action function, which says to WordPress that it must call our function when the wp_enqueue_scripts hook is triggered, which happens during our call to wp_head() in the header.php file.

Inside our function, we have the following lines:

Firstly, we register our CSS and add it to the WordPress queue.

We’ve used the wp_register_style function to register our custom style, and this function has the following arguments:

  • $handle: a name that you can choose—something like mystyle, mediaqueries, etc.
  • $src: the file path
  • $deps: dependencies, the name of the style files that need to be loaded before this file
  • $ver: the asset version
  • $media: the media for which this stylesheet will be loaded

And then, we’ve called the wp_enqueue_style function, and we pass the name of our style in the first argument of the wp_enqueue_style function, so it will be added to the queue.

To add more styles, just duplicate these two lines and modify the name, directory, and the other parameters.

Adding the Scripts

Next, we’ll see the function which adds our scripts.

In this case too, the process is pretty much the same—the only difference is that we’ve used different functions to add scripts.

To add scripts, we’ve used the wp_register_script and wp_enqueue_script functions. The wp_register_script function requires the following arguments:

  • $handle: a name that you can choose—something like jquery, dojo, etc.
  • $src: the file path
  • $deps: dependencies, the name of script files that need to be loaded before this file
  • $ver: the asset version
  • $in_footer: whether to add/enqueue the script before </body> instead of in the <head>

And then, we’ve called the wp_enqueue_script function, and we pass the name of our script in the first argument of the wp_enqueue_script function, so it will be added to the queue.

To add more scripts, just duplicate these two lines and modify the name, directory, and the other parameters.

Understanding the header.php File

The doctype

All pages should begin with the doctype declaration as shown in the following snippet.

The <html> Tag

This is the root of an html document. It’s also important to note that we’ve used the language_attributes function to add the necessary language attributes for the html tag.

The <meta> Tags

The meta tags are very important because they pass certain information to the browser to ensure the correct rendering of your theme.

The above line ensures that browsers won’t use quirks mode, and it’s very useful because this render mode can break the layout.

The above line sets the charset for the browser, avoiding unknown characters!

In the above snippet, we’ve added a few basic meta tags that can help improve the SEO of your website. You can add keywords that describe your website and type your name or business name.

The above tag removes and resets any default zoom of a mobile device like iPad and iPhone, if you are working on a responsive layout.

The above snippet adds a favicon icon to your page, giving a more professional touch to your website.

The above snippet adds a link to the RSS feed of your website.

Finally, you can use the above snippet to add a link to the pingback URL of your site.

The <title> Tag

The title tag is very important because it replaces the default title, and it is useful to improve your rank in search engines.

The wp_head() Function

This is a very important function—you must call this function! Through this function, WordPress adds code from plugins, other JavaScript libraries, and much more.

The Header Section

Finally, for your common header, which displays the logo and the main menu of your website, you can use the div tag as shown in the following snippet.

Conclusion

So that’s how you can use the header.php file to prepare the common header in your WordPress theme.

This post has been updated with contributions from Sajal Soni. Sajal belongs to India, and he loves to spend time creating websites based on open-source frameworks.