Using WordPress ‘get_header_image’ PHP filter

The get_header_image WordPress PHP filter allows you to modify the header image URL.

Usage

add_filter('get_header_image', 'your_custom_function');
function your_custom_function($url) {
    // your custom code here
    return $url;
}

Parameters

  • $url (string): Header image URL to be filtered.

More information

See WordPress Developer Resources: get_header_image

Examples

Change header image for specific pages

Change the header image URL for a specific page or pages using their IDs.

add_filter('get_header_image', 'change_header_image_for_specific_pages');
function change_header_image_for_specific_pages($url) {
    if (is_page(array(10, 25))) { // Replace with your specific page IDs
        $url = 'https://example.com/new-header-image.jpg'; // Replace with your new header image URL
    }
    return $url;
}

Change header image based on post type

Change the header image URL for a specific custom post type.

add_filter('get_header_image', 'change_header_image_for_custom_post_type');
function change_header_image_for_custom_post_type($url) {
    if (is_singular('your_post_type')) { // Replace 'your_post_type' with your custom post type
        $url = 'https://example.com/new-header-image.jpg'; // Replace with your new header image URL
    }
    return $url;
}

Change header image for logged-in users

Change the header image URL for logged-in users only.

add_filter('get_header_image', 'change_header_image_for_logged_in_users');
function change_header_image_for_logged_in_users($url) {
    if (is_user_logged_in()) {
        $url = 'https://example.com/new-header-image.jpg'; // Replace with your new header image URL
    }
    return $url;
}

Change header image based on the day of the week

Display a different header image URL based on the day of the week.

add_filter('get_header_image', 'change_header_image_based_on_day');
function change_header_image_based_on_day($url) {
    $day = date('w'); // Get the current day of the week (0 for Sunday, 1 for Monday, etc.)

    if ($day == 1) { // Change header image for Mondays
        $url = 'https://example.com/monday-header-image.jpg'; // Replace with your new header image URL for Monday
    }
    return $url;
}

Change header image randomly

Display a random header image from an array of image URLs.

add_filter('get_header_image', 'change_header_image_randomly');
function change_header_image_randomly($url) {
    $images = array(
        'https://example.com/header-image-1.jpg',
        'https://example.com/header-image-2.jpg',
        'https://example.com/header-image-3.jpg'
    );
    $random_image = $images[array_rand($images)];
    $url = $random_image;
    return $url;
}