RBA Cash Rate: 4.35% · 1AUD = 0.67 USD · Inflation: 4.1%  
Leading Digital Marketing Experts</strong | 1300 235 433 | Aggregation Enquires Welcome | Book Appointment
Example Interest Rates: Home Loan Variable: 5.69% (5.89%*) • Home Loan Fixed: 5.39% (6.59%*) • Fixed: 5.39% (6.59%*) • Variable: 5.69% (5.89%*) • Investment IO: 5.69% (6.48%*) • Investment PI: 5.39% (6.59%*)

Reference (Additional) Device Favicons in WordPress

Reference (Additional) Device Favicons in WordPress

Further to our article on the importance website favicons, this post the necessary code to include limited Apple Touch, Android, Microsoft, and other icons in your WordPress document head so that they'll be recognized by various devices. This is the first of two articles; this post dealing only with WordPress and doesn't utilize the array of images returned by our Icofav icon creation tool.

Since WordPress version 4.3, the platform provides the functionality to create and automatically reference the four most used .png icons - 32x32 (favicon), 192x192 (Android/Chrome), 180x180 (IOS), and 270x270 (medium sized Windows tile). To upload a favicon image to WordPress, navigate to Appearance -> Customise -> Site Identity. Upload a file, click select, then Save and Publish. The WordPress feature isn't great; it won't create a favicon.ico file in your root directory, and it only renders limited icons in your WordPress page, but it is reasonably adequate.

1
<link rel="icon" href="http://www.YourSite.com/wp-content/uploads/yyyy/mm/cropped-orange-icon-32x32.jpg" sizes="32x32" />
2
<link rel="icon" href="http://www.YourSite.com/wp-content/uploads/yyyy/mm/cropped-orange-icon-192x192.jpg" sizes="192x192" />
3
<link rel="apple-touch-icon-precomposed" href="http://www.YourSite.com/wp-content/uploads/yyyy/mm/cropped-orange-icon-180x180.jpg" />
4
<meta name="msapplication-TileImage" content="http://www.YourSite.com/wp-content/uploads/yyyy/mm/cropped-orange-icon-270x270.jpg" />
5
</head>

While it's only the four images above that are rendered to each page by WordPress to default, uploading an image does create a small suite of commonly used icon images. The code below will add these additional icons to your WordPress website.

Reference (Additional) Device Favicons in WordPress

Images created by WordPress. Default WP icons are marked with a tick.

It's the 60x60, 150x150, 192x192, 300x300, and 512x512 icon that are created by WP by default but omitted from your rendered WordPress page. Before we look at how these icons can be included into your page output, it's necessary to look at the WordPress Icon API.

WordPress Site_Icon API

WordPress introduced four new functions to the public API with their site icon feature:

  • has_site_icon() returns whether the current site has an icon set up or not.
  • wp_site_icon() displays all available favicons and app icons.
  • get_site_icon_url() returns the url to the current site's icon, or the default passed to it.
  • site_icon_url() displays an escaped version of the url to the current site's icon.

has_site_icon()

The has_site_icon() function determines whether the current site has an icon set up or not. It returns a Boolean value based on whether it can retrieve a 512px icon with the get_site_icon_url function (if the 512px image was uploaded it returns true). Because the 512px image is required, smaller images are scaled up when uploading, so it's best to keep to the minimum 512 pixels.

If you're a plugin or theme developer, or you run an agency, you might force the icon upload by displaying an administration message encouraging that action.

Reference (Additional) Device Favicons in WordPress

The code:

Copy and paste the WordPress function into your theme's functions.php file or, if you sensibly have one installed, your custom functions plugin.

1
<?php 
2
/*
3
 Reference (Additional) Favicons in WordPress
4
 http://www.beliefmedia.com/favicons-wordpress
5
*/
6
 
7
function beliefmedia_favicon_admin_message() {
8
 if (!has_site_icon()) {
9
  echo '<div id="message" class="error"><p><strong>You have not uploaded your 512x512px favicon image. You can do so <a href="' . rtrim(network_admin_url(), '/') . '/customize.php">here</a> (Click on Site Identity).</strong></div>';
10
 }
11
}
12
add_action('admin_notices', 'beliefmedia_favicon_admin_message');

If using a multisite the $blog_id should be passed to the function.

get_site_icon_url() & site_icon_url()

The functions get_site_icon_url and site_icon_url are similar. The first function will return the icon URL, while the second applies esc_url to the result of the first function and echoes the URL directly.

wp_site_icon()

The wp_site_icon function loops through the array of default icons and echoes them to the head of your website. Of course, wp_site_icon() is part of the WP core so theme developers no longer have to worry about adding this feature themselves (unless they want to improve upon it).

WordPress only generates a limited number of icons based on the needs of the many so you will likely want to create additional icons. This may be accomplished with the site_icon_image_sizes filter. The function below must be in place before you upload your icons or they won't be generated.

1
<?php 
2
/*
3
 Reference (Additional) Favicons in WordPress
4
 http://www.beliefmedia.com/favicons-wordpress
5
*/
6
 
7
function beliefmedia_favicon_custom_sizes($sizes) {
8
  $sizes[] = 76;
9
  $sizes[] = 120;
10
  $sizes[] = 152;
11
 return $sizes;
12
}
13
add_filter('site_icon_image_sizes', 'beliefmedia_favicon_custom_sizes');

... although I find this cleaner:

1
<?php 
2
/*
3
 Reference (Additional) Favicons in WordPress
4
 http://www.beliefmedia.com/favicons-wordpress
5
*/
6
 
7
function beliefmedia_favicon_custom_sizes($sizes) {
8
  $sizes = array(76,120,152);
9
 return $sizes;
10
}
11
add_filter('site_icon_image_sizes', 'beliefmedia_favicon_custom_sizes');

Both functions are obviously both the same. I've used just three Apple app icon sizes as recommended by Apple, and aren't rendered in the WordPress head (or by their icon generation tool) by default. In reality, you'll want to add multiple sizes.

If you copy this code into your custom functions file and upload an image, the additional images sizes will be created.

Reference (Additional) Device Favicons in WordPress

Now that the additional images exist, we need to reference them in our WordPress head. The site_icon_meta_tags filter is referenced in the wp_site_icon function (that renders the meta tags to the head).

The only attribute that is requires is $tags, otherwise you won't return the default WP images (although if you were re-writing them all it's something you might consider).

Notice that along with the custom sizes referenced for each element in the array, I've also defined the rel tag. You would manufacture the rel tags based on the size and type of icon you're referencing for a particular device.

1
<?php 
2
/*
3
 Reference (Additional) Favicons in WordPress
4
 http://www.beliefmedia.com/favicons-wordpress
5
*/
6
 
7
function beliefmedia_custom_favicon_sizes_head($tags) {
8
  $tags[] = sprintf( '<link rel=&quot;apple-touch-icon&quot; href=&quot;%s&quot; />', esc_url( get_site_icon_url( 60 ) ) );
9
  $tags[] = sprintf( '<link rel=&quot;apple-touch-icon&quot; href=&quot;%s&quot; sizes=&quot;76x76&quot; />', esc_url( get_site_icon_url( 76 ) ) );
10
  $tags[] = sprintf( '<link rel=&quot;apple-touch-icon&quot; href=&quot;%s&quot; sizes=&quot;120x120&quot; />', esc_url( get_site_icon_url( 120 ) ) );
11
  $tags[] = sprintf( '<link rel=&quot;apple-touch-icon&quot; href=&quot;%s&quot; sizes=&quot;152x152&quot; />', esc_url( get_site_icon_url( 152 ) ) );
12
 return $tags;
13
}
14
add_filter('site_icon_meta_tags', 'beliefmedia_custom_favicon_sizes_head');

Note that we've included the 60x60 icon but we didn't include it in our array of new image sizes. This is because it's one of the icons that WordPress creates by default but does not display.

This function would output the following to your document head.

1
<link rel="icon" href="http://www.YourSite.com/wp-content/uploads/yyyy/mm/cropped-blue-icon-60x60.png" sizes="32x32" />
2
<link rel="icon" href="http://www.YourSite.com/wp-content/uploads/yyyy/mm/cropped-blue-icon-300x300.png" sizes="192x192" />
3
<link rel="apple-touch-icon-precomposed" href="http://www.YourSite.com/wp-content/uploads/yyyy/mm/cropped-blue-icon-300x300.png" />
4
<meta name="msapplication-TileImage" content="http://www.YourSite.com/wp-content/uploads/yyyy/mm/cropped-blue-icon-300x300.png" />
5
<link rel="apple-touch-icon" href="http://www.YourSite.com/wp-content/uploads/yyyy/mm/cropped-blue-icon-60x60.png" />
6
<link rel="apple-touch-icon" href="http://www.YourSite.com/wp-content/uploads/yyyy/mm/cropped-blue-icon-76x76.png" sizes="76x76" />
7
<link rel="apple-touch-icon" href="http://www.YourSite.com/wp-content/uploads/yyyy/mm/cropped-blue-icon-120x120.png" sizes="120x120" />
8
<link rel="apple-touch-icon" href="http://www.YourSite.com/wp-content/uploads/yyyy/mm/cropped-blue-icon-152x152.png" sizes="152x152" />

Considerations

Despite the proliferation of png favicon files, particularly over the last few years with the advent of the mobile and tablet overlords, the ico file still does have a place. One should be created (with icofav.com ) and left in your root directory with a 16x16 .png file of the same image. The ico file we generate on icofav is more of a container for a number of icon images, and it's still used by a large number of applications. It's good practice to have them there - even if they're not referenced in your document head.

The Favicon Series

In a forthcoming favicon post we look at how you can manufacture a complete library of icons with our Icofav tool and reference them all into your WordPress or PHP website.

  1. The Importance of a Website FaviconJuly 23, 2017
  2. Reference (Additional) Device Favicons in WordPressJuly 26, 2017
  3. Favicons with Outgoing Links in WordPress (or PHP) With Google’s Icon APIJuly 27, 2017
  4. One-Click Website Favicons and Automated MaintenanceApril 13, 2021

■ ■ ■

 
Download our complimentary 650-page guide on marketing for mortgage brokers. We'll show you exactly how we generate billions in volume for our clients.
Finance Guide, Cropped Top and Bottom
  Timezone: 1 · [ CHANGE ]

RELATED READING

Like this article?

Share on Facebook
Share on Twitter
Share on Linkdin
Share on Pinterest

Leave a comment