is_favicon()
Checks if the current request is a request for the favicon.ico file.
This function is called by default when loading the site template page in the file wp-includes/template-loader.php
The earliest moment this function can be called is the hooks: parse_query, wp, template_redirect or later.
See also the hook do_favicon.
The function will trigger when the URL /favicon.ico is requested
This usually happens automatically when the site loads to display the tab icon:
- A regular user visit to the site — the browser itself makes a request to
/favicon.ico. - A bot (for example, a search engine) indexes the site and requests the icon.
- A direct link like
https://example.com/favicon.ico.
The function may not trigger if:
- The favicon is requested via a different path (for example, specified in the meta tag
<link rel="icon" href="https://example.com/custom-icon.ico">). - The request bypasses WordPress — for example, if the server (Nginx/Apache) serves
favicon.icodirectly, bypassing index.php. - Server-level caching (Nginx, Varnish, ...) is used, and the favicon is served without WP involvement.
- WP is installed in a subdirectory, and the favicon is requested from the root (/favicon.ico), where WP is not present.
-
A separate domain or CDN is used for static files, and the icon is loaded from there.
-
WP initialization is intercepted, for example, via auto_prepend_file — this is a PHP directive that allows including a file before executing any other PHP code.
Example in php.ini or .htaccess:
php_value auto_prepend_file /path/to/file.php
If the following condition is set in this file, then WP will not load at all when requesting /favicon.ico:
if ( $_SERVER['REQUEST_URI'] === '/favicon.ico' ) { readfile( '/path/to/favicon.ico' ); exit; }
No Hooks.
Returns
true|false. Whether the favicon.ico file is requested.
Usage
if( is_favicon() ){
// do staff
}
Examples
#1 Generating an icon on the fly
This code will return its favicon.ico and terminate execution when the browser requests /favicon.ico.
The icon is generated on the fly using GD and output as a favicon (16x16, black-and-white pattern):
add_action( 'do_favicon', 'get_simple_favicon' );
function get_simple_favicon() {
header( 'Content-Type: image/x-icon' );
$size = 16;
$img = imagecreatetruecolor( $size, $size );
$white = imagecolorallocate( $img, 255, 255, 255 );
$black = imagecolorallocate( $img, 0, 0, 0 );
// Drawing a simple checkerboard pattern
for ( $y = 0; $y < $size; $y++ ) {
for ( $x = 0; $x < $size; $x++ ) {
$color = ( ( $x + $y ) % 2 === 0 ) ? $black : $white;
imagesetpixel( $img, $x, $y, $color );
}
}
// Saving the image .ico to a variable
ob_start();
imagepng( $img );
imagedestroy( $img );
$png_data = ob_get_clean();
// Outputting to the screen (converting PNG to ICO via header)
echo $png_data;
exit;
} #2 Examples of using the is_favicon() function
Although this function is used automatically in WP in the file wp-includes/template-loader.php, it can still be useful at times.
For example, we want to log all favicon requests separately:
if ( is_favicon() ) {
error_log( 'Favicon requested: ' . $_SERVER['REMOTE_ADDR'] );
}
Notes
- Global. WP_Query.
$wp_queryWordPress Query object.
Changelog
| Since 5.4.0 | Introduced. |
is_favicon() is favicon code WP 7.0
function is_favicon() {
global $wp_query;
if ( ! isset( $wp_query ) ) {
_doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1.0' );
return false;
}
return $wp_query->is_favicon();
}