All about wp_head() Function Details

All about wp_head() Function Details

The wp_head() function is one of the most important WordPress hooks. It’s used in themes to output dynamic content inside the <head> section of every page — including scripts, styles, meta tags, and other elements injected by plugins and WordPress core.


What Does wp_head() Do?

wp_head() fires the wp_head action hook, which allows plugins, themes, and WordPress itself to inject content into the <head> of your HTML document. Without it, many plugins simply won’t work.


Where to Place wp_head()

Add <?php wp_head(); ?> to your theme’s header.php file, right before the closing </head> tag:

<!DOCTYPE html>
<html <?php language_attributes(); ?>>
<head>
  <meta charset="<?php bloginfo('charset'); ?>">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title><?php wp_title('|', true, 'right'); ?></title>
  <?php wp_head(); ?>
</head>
<body <?php body_class(); ?>>

Important: wp_head() must be placed in <head> — if it’s missing, plugins can’t enqueue stylesheets or JavaScript.


What Gets Output by wp_head()

The content printed by wp_head() varies depending on your setup. Common outputs include:

  • WordPress core: <meta> tags, <link> to RSD, REST API links, emoji scripts
  • Plugins: Analytics scripts, custom CSS, SEO meta tags, conversion tracking
  • Themes: Custom fonts, inline critical CSS, preload directives

Why wp_head() Is Critical

  • Plugin compatibility: Most plugins rely on this hook to add their scripts and styles.
  • SEO plugins: Yoast SEO and Rank Math inject meta tags here.
  • Performance tools: Caching and CDN plugins use it for optimization hints.
  • Custom integrations: Google Analytics, Facebook Pixel, and other tracking scripts depend on it.

Common wp_head() Outputs by Default

Here’s what WordPress prints by default:

// REST API link
<link rel="https://api.w.org/" href="https://example.com/wp-json/" />

// RSD (Really Simple Discovery) link
<link rel="EditURI" type="application/rsd+xml" title="RSD" href="https://example.com/xmlrpc.php?rsd" />

// Shortlink
<link rel="shortlink" href="https://example.com/?p=123" />

// WordPress version (visible in source)
<meta name="generator" content="WordPress 6.4" />

Removing Unwanted wp_head() Output

If you want to clean up the <head> and remove default WordPress outputs, use remove_action() in your functions.php:

remove_action('wp_head', 'wp_generator');
remove_action('wp_head', 'wlwmanifest_link');
remove_action('wp_head', 'rsd_link');
remove_action('wp_head', 'wp_shortlink_wp_head');
remove_action('wp_head', 'feed_links_extra', 3);

Best Practices

  1. Always include wp_head() in your theme’s header.php
  2. Use wp_enqueue_scripts instead of hardcoding <script> or <link> tags
  3. Combine and minify CSS/JS where possible (defer non-critical scripts)
  4. Remove unused outputs to keep your <head> clean for performance

Removing wp_head() — When (Not) to Do It

Some developers remove wp_head() in custom themes to reduce bloat. This is only recommended if:

  • You fully control every script and stylesheet in your theme
  • You don’t use plugins that depend on WordPress hooks
  • You understand the performance and compatibility implications

For most WordPress sites, leaving wp_head() intact is the safest choice.


Conclusion

wp_head() is a fundamental WordPress function. It enables plugins, themes, and WordPress core to properly inject assets into your page <head>. Always keep it in your theme’s header.php unless you have a very specific reason to remove it.

Understanding how wp_head() works helps you build better themes and debug issues with missing styles, broken scripts, or plugin conflicts.


  • wp_footer() — Similar hook for outputting content before </body>
  • wp_enqueue_script() — Proper way to add JavaScript
  • wp_enqueue_style() — Proper way to add CSS
  • add_action() — How plugins hook into wp_head
Share:

Get Started on Creating Your Web Apps
with a Reliable Service Today!

Expert WordPress, Shopify & WooCommerce development — on time, every time.

Get a Free Quote →