wp_footer
Fires in the page footer. Scripts, styles, and other assets are commonly included when this event fires. This is one of the main theme hooks.
In addition to themes, WordPress itself and many plugins actively use this event.
This event is triggered by the wp_footer() function of the same name, which is called in the theme's footer.php file.
Calling wp_footer() is mandatory for all WordPress themes (templates). It is done as follows:
... <?php wp_footer(); ?> </body> </html>
Many built-in WordPress functions fire during this event. For example, one of them adds scripts registered through wp_enqueue_script() that were configured to load in the footer.
What fires on this hook on the front end by default in WordPress:
add_action( 'wp_footer', 'wp_print_footer_scripts', 20 ); add_action( 'wp_print_footer_scripts', '_wp_footer_scripts' ); add_action( 'wp_footer', 'wp_admin_bar_render', 1000 );
There is another similar event, wp_head. It is triggered by wp_head() in the document's HEAD section, in header.php.
Usage
add_action( 'wp_footer', 'wp_kama_footer_action' );
/**
* Function for `wp_footer` action-hook.
*
* @return void
*/
function wp_kama_footer_action(){
// action...
}
Examples
#1 Add HTML to the footer
Suppose a popup's HTML must be added to every page:
<?php
add_action( 'wp_footer', 'my_popup', 30 );
function my_popup(){
?>
<div id="my_popup" class="popup mfp-hide">
<div class="popup_inner">
<div class="popup_body">
<div class="popup_content"></div>
</div>
</div>
</div>
<?php
}
#2 Output custom JavaScript in the footer
This demonstrates how to output JavaScript at the end of the HTML on any site page:
## JavaScript at the end of the document.
add_action( 'wp_footer', 'hook_javascript', 99 );
function hook_javascript(){
?>
<script> alert('Page is loaded.'); </script>
<?php
}
Or this can be used:
## JavaScript at the end of the document.
add_action( 'wp_print_footer_scripts', 'hook_javascript' );
function hook_javascript(){
?>
<script> alert('Page is loaded.'); </script>
<?php
}Changelog
| Since 1.5.1 | Introduced. |
Where the hook is called
do_action( 'wp_footer' );
Where the hook is used in WordPress
add_action( 'wp_footer', array( 'WP_Duotone', 'output_footer_assets' ), 10 );
add_action( 'wp_footer', 'block_core_image_print_lightbox_overlay' );
add_action( 'wp_footer', array( $this, 'customize_preview_settings' ), 20 );
add_action( 'wp_footer', array( $this, 'export_preview_data' ), 1 );
add_action( 'wp_footer', array( $this, 'export_preview_data' ), 20 );
add_action( 'wp_footer', array( $this, 'print_enqueued_script_modules' ) );
add_action( 'wp_footer', array( $this, 'print_script_module_translations' ), 21 );
add_action( 'wp_footer', array( $this, 'print_script_module_data' ) );
add_action( 'wp_footer', array( $this, 'print_a11y_script_module_html' ), 20 );
add_action( 'wp_footer', array( $this, 'export_preview_data' ), 1000 );
add_action( 'wp_footer', 'wp_print_speculation_rules' );
add_action( 'wp_footer', 'wp_print_footer_scripts', 20 );
add_action( 'wp_footer', 'wp_enqueue_global_styles', 1 );
add_action( 'wp_footer', 'wp_enqueue_stored_styles', 1 );
add_action( 'wp_footer', 'wp_maybe_inline_styles', 1 ); // Run for late-loaded styles in the footer.
add_action( 'wp_footer', 'wp_admin_bar_render', 1000 ); // Back-compat for themes not using `wp_body_open`.
add_action( 'wp_footer', 'the_block_template_skip_link' ); // Retained for backwards-compatibility. Unhooked by wp_enqueue_block_template_skip_link().
add_action( 'wp_footer', array( $this, 'print_router_markup' ) );
add_action( 'wp_footer', 'wp_underscore_playlist_templates', 0 );
add_action( 'wp_footer', 'wp_print_media_templates' );
add_action( 'wp_footer', $capture_late_styles, 20 );
remove_action( 'wp_footer', 'the_block_template_skip_link' );