black friday sale

In the header and footer of the WordPress website, you can add custom code such as tracking scripts, meta tags, or custom CSS to connect with external services. Today, let’s try a simple way to add code to the WordPress header and footer with the help of MB Views from Meta Box.

Video Version

Why MB Views?

To integrate your websites with external services like Google Analytics, Google Search Console, and Facebook Pixel, etc. You usually should add code to the header and footer of the website.

To do it, you can add code to the theme directly. But, it is not an optimal option. We recommend using plugins or third-party services that can help to do that indirectly. It brings two significant benefits:

  • The code will be not affected when you change the theme.
  • You can manage the code more easily.

MB Views is one of those plugins. It will come handy especially when you have Meta Box on your site.

For the header and footer, MB Views supports two actions: wp_head and wp_footer. So, your work is just inserting the code into the view, then using one of these actions. Then, everything will be done.

In this blog, I’m giving some examples of adding code to the header and footer, including adding or changing the content in the header or footer, and adding Google Analytics and Google Tag Manager scripts.

Before Getting Started

First, we need Meta Box to have the framework for using MB Views. You can download it directly from wordpress.org.

As I mentioned before, we also need MB Views to add code to the header and footer. It is an extension from Meta Box, which allows you to get Meta Box fields and build your templates on the front end fast and easily. But, you also can use it as a tool to add code to your theme. If you have the Meta Box AIO, please notice that it’s also included in the package.

Add or Change the Content in the Header

We will create a view as usual, then use the two actions I mentioned to define them in the header or footer.

Go to Meta Box > Views and create a new view.

Go to Meta Box > Views to create a new view.

Add any code you want to declare, get data, style, or anything else...in three tabs provided by MB Views.

Add any code you want into the Template, CSS, JavaScript tabs

For example, in the Template tab, I add some code:

<meta name="description" content="Meta Box plugin">
<link rel="stylesheet" href="styles.css">

In the Template tab, add some code as an example

In there, <meta name="description" content="Meta Box plugin"> is to add a description for the meta tag with the content as “Meta Box plugin”.

I also use CSS to style the menu in the header a little bit.

Also use CSS tab to style

In the Javascript tab, I add a line to display a notification.

In the Javascript tab, I add a line to display a notification

After adding the code as you want, scroll down to the Settings section.

To add all the above code to the header or footer, we should set the type for this view as Action. Then, enter the action in the Action name box.

Set the type for this view as Action and enter the action in the Action name box

After publishing, on the frontend, you’ll see the alert which has content specified in the JavaScript tab before.

There’s an alert displayed

The menu on the header also turns to red as styling.

The menu on the header also turns to red as styling

Add or Change Content in the Footer

Do the same with the footer.

In another view, add code to the tabs.

Add some code into three tabs

Then, instead of using the action for the header, just use the wp_footer action to apply the code to the footer of the website.

Use the wp_footer action to apply the code to the footer of the website

Go to the page on frontend. There is a notification since I also added it in the JavaScript tab.

There is a notification on the frontend

Also, the footer has a text section in black.

The footer has a text section in black

Add Code to Connect with External Services

For a clearer look, let's add the code to connect the website with external services.

Not only changing the content and styling something on your pages, you can also use the MB Views to add scripts from third parties such as Google, Facebook, Hotjar, etc. I’ll link the header with Google Analytics and Google Tag Manager as examples.

Add Google Analytics Code Snippet

For Google Analytics, after getting the code snippet from your Google account, go back to your site, and create a new view as well.

Create a new view to add Google Analytics code snippet

Paste the script to the JavaScript tab.

Paste the script to the JavaScript tab

Then set the Type as Action, and name the action as wp_head to add the script to the header of the page.

Set the Type as Action, and name the action as wp_head to add the script to the header of the page

That’s done.

Add Google Tag Manager Code Snippet

Do likewise with the Google Tag Manager code snippet. Get it from your Google account.

Then, add the script to the JavaScript tab in a view.

Add the script of Google Tag Manager to the JavaScript tab

And, set the type of the view as action as well. We also should use the wp_head action in this case.

Set the type of the view using the wp_head action

That’s so simple.

Last Words

With the help of MB Views, adding code to the header and footer is extremely easy. The applications of these actions are not just that. For example, you can add a few settings for the wp-head action to create the dynamic favicon for the WordPress website.

Besides the wp-head and wp_footer hooks, you can use some other hooks provided by WordPress to conform to your needs.

I hope this tip is useful to you. If you have any questions or requests for the next tutorials, feel free in the comment. Thanks for reading!

Leave a Reply

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