wp_footer

March 09, 2023
Dictionary - Projects Engine

wp_footer is a WordPress hook that is used to inject code or scripts into the footer section of a WordPress website. It is one of the core hooks in WordPress and is typically included in the theme’s footer.php file.

The wp_footer hook is called at the end of the HTML document, just before the closing </body> tag. It is often used to add JavaScript or other tracking codes that need to be included in the footer of the website.

Here’s an example of how to use wp_footer hook to add Google Analytics tracking code to your WordPress website:

function add_google_analytics() {
?>
  <!-- Google Analytics tracking code -->
  <script async src="https://www.googletagmanager.com/gtag/js?id=GA_MEASUREMENT_ID"></script>
  <script>
    window.dataLayer = window.dataLayer || [];
    function gtag(){dataLayer.push(arguments);}
    gtag('js', new Date());

    gtag('config', 'GA_MEASUREMENT_ID');
  </script>
  <!-- End Google Analytics tracking code -->
<?php
}
add_action('wp_footer', 'add_google_analytics');

In this example, we’ve created a function called add_google_analytics that contains the Google Analytics tracking code. We then use the add_action function to register this function with the wp_footer hook.

This code will add the Google Analytics tracking code to the footer of every page on your WordPress website.

Was this article helpful?