Using WordPress ‘print_footer_scripts()’ PHP function

The print_footer_scripts() WordPress PHP function prints the scripts that were queued for the footer or too late for the HTML head.

Usage

print_footer_scripts();

Parameters

  • None

More information

See WordPress Developer Resources: print_footer_scripts
This function was introduced in WordPress version 2.8.

Examples

Basic usage

Print the footer scripts in your theme’s footer.php file.

// In your theme's footer.php file
print_footer_scripts();

Custom action hook

Create a custom action hook to print footer scripts after your theme’s footer content.

// In your theme's functions.php file
function my_theme_footer_scripts() {
  print_footer_scripts();
}
add_action('my_theme_after_footer', 'my_theme_footer_scripts');

// In your theme's footer.php file
do_action('my_theme_after_footer');

Enqueue a custom JavaScript file and print it using print_footer_scripts().

// In your theme's functions.php file
function my_theme_enqueue_scripts() {
  wp_enqueue_script('my-theme-custom-js', get_template_directory_uri() . '/js/custom.js', array(), '1.0.0', true);
}
add_action('wp_enqueue_scripts', 'my_theme_enqueue_scripts');

// In your theme's footer.php file
print_footer_scripts();

Dequeue an unnecessary script before printing footer scripts.

// In your theme's functions.php file
function my_theme_dequeue_scripts() {
  wp_dequeue_script('unnecessary-script-handle');
}
add_action('wp_print_footer_scripts', 'my_theme_dequeue_scripts', 9);

// In your theme's footer.php file
print_footer_scripts();

Add a custom script tag to the footer using print_footer_scripts().

// In your theme's functions.php file
function my_theme_add_custom_script_tag() {
  echo "<script>console.log('Hello, footer!');</script>";
}
add_action('wp_print_footer_scripts', 'my_theme_add_custom_script_tag');

// In your theme's footer.php file
print_footer_scripts();