Using WordPress ‘do_activate_header()’ PHP function

The do_activate_header() is a WordPress PHP function that adds an action hook specifically for the current page. It’s typically used to fire on ‘wp_head’.

Usage

Here’s an example of how you might use do_activate_header() in your WordPress theme:

function my_custom_header() {
    echo '<meta name="description" content="My custom header description">';
}

add_action('wp_head', 'my_custom_header');

In this example, we’re creating a function called my_custom_header() that outputs a custom meta description tag. We then use add_action() to attach this function to the ‘wp_head’ hook, which is fired by do_activate_header().

Parameters

  • This function doesn’t accept any parameters.

More information

See WordPress Developer Resources: do_activate_header()

Note that the do_activate_header() function is part of the WordPress core and is not deprecated as of the latest WordPress version.

Examples

Add a Custom CSS File to the Header

function enqueue_custom_css() {
    wp_enqueue_style('my-custom-css', get_template_directory_uri() . '/css/custom.css');
}
add_action('wp_head', 'enqueue_custom_css');

This attaches the enqueue_custom_css() function to the ‘wp_head’ hook. The function adds a custom CSS file located in the theme’s css directory.

Set a Custom Favicon

function set_custom_favicon() {
    echo '<link rel="shortcut icon" href="' . get_stylesheet_directory_uri() . '/favicon.ico" />';
}
add_action('wp_head', 'set_custom_favicon');

This adds a custom favicon to your WordPress site by inserting a link tag into the header.

Add Google Analytics Tracking Code

function add_ga_tracking_code() {
    echo '<script async src="https://www.googletagmanager.com/gtag/js?id=UA-XXXXXXXXX-X"></script>';
}
add_action('wp_head', 'add_ga_tracking_code');

This inserts the Google Analytics tracking script into the header of your site. Remember to replace ‘UA-XXXXXXXXX-X’ with your own Tracking ID.

Set a Custom Page Description

function set_custom_page_description() {
    if (is_single()) {
        $description = get_the_excerpt();
        echo '<meta name="description" content="' . $description . '">';
    }
}
add_action('wp_head', 'set_custom_page_description');

This function checks if the current page is a single post or page, and if so, it sets the meta description to the excerpt of the post.

Add Custom JavaScript

function add_custom_js() {
    echo '<script src="' . get_template_directory_uri() . '/js/custom.js"></script>';
}
add_action('wp_head', 'add_custom_js');

This function adds a custom JavaScript file from your theme’s js directory to the header of your WordPress site.