do_action( 'wp_head' )

Prints scripts or data in the head tag on the front end.


Description Description


Source Source

File: wp-includes/general-template.php

View on Trac


Top ↑

Changelog Changelog

Changelog
Version Description
1.5.0 Introduced.


Top ↑

User Contributed Notes User Contributed Notes

  1. Skip to note 1 content
    Contributed by Aurovrata Venet

    taken from the old codex pages:

    function hook_css() {
        ?>
            <style>
                .wp_head_example {
                    background-color : #f1f1f1;
                }
            </style>
        <?php
    }
    add_action('wp_head', 'hook_css');

    or for inline scripts which need to be placed in the head,

    function hook_javascript() {
        ?>
            <script>
                alert('Page is loading...');
            </script>
        <?php
    }
    add_action('wp_head', 'hook_javascript');

    The wp_head() function which ones sees in all header.php files, is simply triggering the hool do_action(‘wp_head’). WordPress core files then hooks it multiple times to print the head,

    add_action( 'wp_head',             '_wp_render_title_tag',            1     );
    add_action( 'wp_head',             'wp_enqueue_scripts',              1     );
    add_action( 'wp_head',             'wp_resource_hints',               2     );
    add_action( 'wp_head',             'feed_links',                      2     );
    add_action( 'wp_head',             'feed_links_extra',                3     );
    add_action( 'wp_head',             'rsd_link'                               );
    add_action( 'wp_head',             'wlwmanifest_link'                       );
    add_action( 'wp_head',             'adjacent_posts_rel_link_wp_head', 10, 0 );
    add_action( 'wp_head',             'locale_stylesheet'                      );
    add_action( 'wp_head',             'noindex',                          1    );
    add_action( 'wp_head',             'print_emoji_detection_script',     7    );
    add_action( 'wp_head',             'wp_print_styles',                  8    );
    add_action( 'wp_head',             'wp_print_head_scripts',            9    );
    add_action( 'wp_head',             'wp_generator'                           );
    add_action( 'wp_head',             'rel_canonical'                          );
    add_action( 'wp_head',             'wp_shortlink_wp_head',            10, 0 );
    add_action( 'wp_head',             'wp_custom_css_cb',                101   );
    add_action( 'wp_head',             'wp_site_icon',                    99    );

    However, more interestingly you can also use it to add some meta tags,

    function hook_nocache() {
        ?>
      <meta http-equiv="cache-control" content="max-age=0" />
      <meta http-equiv="cache-control" content="no-cache" />
      <meta http-equiv="expires" content="0" />
      <meta http-equiv="expires" content="Tue, 01 Jan 1980 1:00:00 GMT" />
      <meta http-equiv="pragma" content="no-cache" />
        <?php
    }
    add_action('wp_head', 'hook_nocache');

You must log in before being able to contribute a note or feedback.