wp_site_icon()WP 4.3.0

Display site icon meta tags.

Hooks from the function

Return

null. Nothing (null).

Usage

wp_site_icon();

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.4.3

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";
	}
}