wp_site_icon()WP 4.3.0

Outputs the meta tag for the site icon: <link rel="icon" href="%s" sizes="32x32" />

Outputs 4 meta tags for different devices:

<link rel="icon" href="%s" sizes="32x32" />
<link rel="icon" href="%s" sizes="192x192" />
<link rel="apple-touch-icon-precomposed" href="%s">
<meta name="msapplication-TileImage" content="%s">

The function is used in the default WP filter, since version 4.3:

add_action( 'wp_head', 'wp_site_icon', 99 );

Before outputting the code, the function checks whether an icon is set for the site. The check for whether there is an icon or not essentially comes down to checking whether there is an option or not...

The attachment ID of the site icon is stored in the option 'site_icon':

// for MU
$site_icon_id = get_blog_option( $blog_id, 'site_icon' );
// regular installation
$site_icon_id = get_option( 'site_icon' );
Hooks from the function

Returns

null. Outputs HTML code.

{{Usage}}

Examples

0

#1 Demo

Suppose we set the icon for the site in: Appearance > Customize > Site Identity, then the following code will work like this:

wp_site_icon();

/* will output:
<link rel="icon" href="http://example.com/wp-content/uploads/2015/07/cropped-gorchitca___-32x32.jpg" sizes="32x32" />
<link rel="icon" href="http://example.com/wp-content/uploads/2015/07/cropped-gorchitca___-192x192.jpg" sizes="192x192" />
<link rel="apple-touch-icon-precomposed" href="http://example.com/wp-content/uploads/2015/07/cropped-gorchitca___-180x180.jpg">
<meta name="msapplication-TileImage" content="http://example.com/wp-content/uploads/2015/07/cropped-gorchitca___-270x270.jpg">
*/
0

#2 Disable the output of the site icon, if it is installed

To do this, we need write following code in the themes functions.php file:

remove_action( 'wp_head', 'wp_site_icon', 99 );

Changelog

Since 4.3.0 Introduced.

wp_site_icon() code WP 6.9.1

function wp_site_icon() {
	if ( ! has_site_icon() && ! is_customize_preview() ) {
		return;
	}

	$meta_tags = array();
	$icon_32   = get_site_icon_url( 32 );
	if ( empty( $icon_32 ) && is_customize_preview() ) {
		$icon_32 = '/favicon.ico'; // Serve default favicon URL in customizer so element can be updated for preview.
	}
	if ( $icon_32 ) {
		$meta_tags[] = sprintf( '<link rel="icon" href="%s" sizes="32x32" />', esc_url( $icon_32 ) );
	}
	$icon_192 = get_site_icon_url( 192 );
	if ( $icon_192 ) {
		$meta_tags[] = sprintf( '<link rel="icon" href="%s" sizes="192x192" />', esc_url( $icon_192 ) );
	}
	$icon_180 = get_site_icon_url( 180 );
	if ( $icon_180 ) {
		$meta_tags[] = sprintf( '<link rel="apple-touch-icon" href="%s" />', esc_url( $icon_180 ) );
	}
	$icon_270 = get_site_icon_url( 270 );
	if ( $icon_270 ) {
		$meta_tags[] = sprintf( '<meta name="msapplication-TileImage" content="%s" />', esc_url( $icon_270 ) );
	}

	/**
	 * Filters the site icon meta tags, so plugins can add their own.
	 *
	 * @since 4.3.0
	 *
	 * @param string[] $meta_tags Array of Site Icon meta tags.
	 */
	$meta_tags = apply_filters( 'site_icon_meta_tags', $meta_tags );
	$meta_tags = array_filter( $meta_tags );

	foreach ( $meta_tags as $meta_tag ) {
		echo "$meta_tag\n";
	}
}