Created
July 9, 2019 13:28
-
-
Save morgyface/cacb65257cb90399a52227667a020f52 to your computer and use it in GitHub Desktop.
WordPress | Favicon list function
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| function add_favicons() { | |
| /** | |
| * Looks in the theme's favicon folder for ico and png files, if files exist | |
| * a list of favicons will be generated. | |
| * | |
| * To cover most devices, copy four files into the folder: | |
| * A standard favicon.ico which is a compilation of three PNG's 16, 32 and 48. | |
| * Two Apple device favicons favicon-152.png and favicon-120.png | |
| * A Microsoft favicon favicon-144.png | |
| * All icons should have a height equalling the width. | |
| */ | |
| $favicon_folder = 'favicon/'; | |
| $favicon_directory = wp_normalize_path( get_template_directory() . '/' . $favicon_folder ); | |
| $favicons = glob( $favicon_directory . '*.{ico,png}', GLOB_BRACE ); | |
| if( $favicons ) { | |
| $output = ''; | |
| foreach( $favicons as $favicon ) { | |
| $path_parts = pathinfo( $favicon ); | |
| $favicon_ext = $path_parts['extension']; | |
| if( $favicon_ext == 'ico' ) { | |
| $rel = 'shortcut icon'; | |
| $sizes = null; | |
| } else { | |
| $dimensions = getimagesize($favicon); | |
| $favicon_width = $dimensions[0]; | |
| if ( $favicon_width == '152' || $favicon_width == '120' ) { | |
| $rel = 'apple-touch-icon-precomposed'; | |
| $sizes = 'sizes="' . $favicon_width . 'x' . $favicon_width . '"'; | |
| } elseif ( $favicon_width == '144' ) { | |
| $name = 'msapplication-TileImage'; | |
| $rel = null; | |
| } | |
| } | |
| $favicon = get_theme_file_uri( $favicon_folder . basename( $favicon ) ); | |
| if( $rel ) { | |
| $output .= '<link rel="' . $rel . '" '; | |
| if( $sizes ) { | |
| $output .= $sizes . ' '; | |
| } | |
| $output .= 'href="' . $favicon . '" />'; | |
| } else { | |
| $output .= '<meta name="' . $name . '" content="' . $favicon . '">'; | |
| } | |
| $output .= PHP_EOL; | |
| } | |
| echo $output; | |
| } | |
| } | |
| add_action( 'wp_head', 'add_favicons' ); |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Favicon for your WordPress theme
I created this as having a list of dead favicon in my header.php file was bugging me.
The concept is:
faviconin your theme;functions.phpfile;<head>of your pages viawp_head.This function works best with the following range of favicon files:
Using the above naming convention i.e favicon-size is a good idea but not essential as the function goes off the image dimensions not the filename.
I manually include a tile color meta tag to:
<meta name="msapplication-TileColor" content="#FFFFFF">I use this online tool to generate my
.icofile. I've also thought about using this clever library to generate the ico file from the png files but it'll have to wait, I've got proper work to do.I've recreated this as a less integrated function right here
The origins of both these are here.