Skip to content

Instantly share code, notes, and snippets.

@schalkburger
Last active January 16, 2020 01:33
Show Gist options
  • Save schalkburger/46862231f41702018e6f46e65b360297 to your computer and use it in GitHub Desktop.
Save schalkburger/46862231f41702018e6f46e65b360297 to your computer and use it in GitHub Desktop.
Roots Sage Favicons
/**
* Load favicons
* Place icons in the theme images directory. Want more? http://realfavicongenerator.net/
*/
function sage_favicons() {
echo '<link rel="apple-touch-icon" sizes="57x57" href="'. get_template_directory_uri() .'/dist/images/apple-touch-icon-57x57.png">';echo "\n";
echo '<link rel="apple-touch-icon" sizes="60x60" href="'. get_template_directory_uri() .'/dist/images/apple-touch-icon-60x60.png">';echo "\n";
echo '<link rel="apple-touch-icon" sizes="72x72" href="'. get_template_directory_uri() .'/dist/images/apple-touch-icon-72x72.png">';echo "\n";
echo '<link rel="apple-touch-icon" sizes="76x76" href="'. get_template_directory_uri() .'/dist/images/apple-touch-icon-76x76.png">';echo "\n";
echo '<link rel="apple-touch-icon" sizes="114x114" href="'. get_template_directory_uri() .'/dist/images/apple-touch-icon-114x114.png">';echo "\n";
echo '<link rel="apple-touch-icon" sizes="120x120" href="'. get_template_directory_uri() .'/dist/images/apple-touch-icon-120x120.png">';echo "\n";
echo '<link rel="apple-touch-icon" sizes="144x144" href="'. get_template_directory_uri() .'/dist/images/apple-touch-icon-144x144.png">';echo "\n";
echo '<link rel="apple-touch-icon" sizes="152x152" href="'. get_template_directory_uri() .'/dist/images/apple-touch-icon-152x152.png">';echo "\n";
echo '<link rel="apple-touch-icon" sizes="180x180" href="'. get_template_directory_uri() .'/dist/images/apple-touch-icon-180x180.png">';echo "\n";
echo '<link rel="icon" type="image/png" href="'. get_template_directory_uri() .'/dist/images/favicon-32x32.png" sizes="32x32">';echo "\n";
echo '<link rel="icon" type="image/png" href="'. get_template_directory_uri() .'/dist/images/favicon-194x194.png" sizes="194x194">';echo "\n";
echo '<link rel="icon" type="image/png" href="'. get_template_directory_uri() .'/dist/images/favicon-96x96.png" sizes="96x96">';echo "\n";
echo '<link rel="icon" type="image/png" href="'. get_template_directory_uri() .'/dist/images/android-chrome-192x192.png" sizes="192x192">';echo "\n";
echo '<link rel="icon" type="image/png" href="'. get_template_directory_uri() .'/dist/images/favicon-16x16.png" sizes="16x16">';echo "\n";
echo '<link rel="mask-icon" href="'. get_template_directory_uri() .'/dist/images/safari-pinned-tab.svg" color="#5bbad5">';echo "\n";
echo '<link rel="shortcut icon" href="'. get_template_directory_uri() .'/dist/images/favicon.ico">';echo "\n";
echo '<meta name="apple-mobile-web-app-title" content="">';echo "\n";
echo '<meta name="application-name" content="">';echo "\n";
echo '<meta name="msapplication-TileColor" content="#603cba">';echo "\n";
echo '<meta name="msapplication-TileImage" content="'. get_template_directory_uri() .'/dist/images/mstile-144x144.png">';echo "\n";
echo '<meta name="msapplication-config" content="'. get_template_directory_uri() .'/dist/images/browserconfig.xml">';echo "\n";
echo '<meta name="theme-color" content="#ecf0f1">';echo "\n";
}
add_action('wp_head', __NAMESPACE__ . '\\sage_favicons');
@tnog
Copy link

tnog commented Oct 11, 2019

Note for other folks for Sage 9, I wouldn't recommend using this approach serving images form /dist/images since the build process will append a unique cache busting string to the file name. Better to probably just add a folder in webroot /icon etc., and then retrieve the path using get_site_url().

You could setup webpack to exclude the files, or a folder from having the cache busting string, but this seems like more work than necessary.

@tnog
Copy link

tnog commented Oct 11, 2019

Also, for Apache based servers /icons in webroot will most likely not work, you'll need to name the directory /webicons or something else.

@figureone
Copy link

figureone commented Jan 16, 2020

Here's another variant that skips the asset pipeline. Edit $favicon_path for wherever you placed them (as written, it's /resources/assets/images/favicons/).

/**
 * Add favicons to <head>.
 * See: https://gist.github.com/schalkburger/46862231f41702018e6f46e65b360297
 */
add_action('wp_head', function () {
    $favicon_path = get_template_directory_uri() . '/assets/images/favicons';
    ?>
    <link rel="apple-touch-icon" href="<?php echo $favicon_path; ?>/apple-icon-57x57.png" sizes="57x57">
    <link rel="apple-touch-icon" href="<?php echo $favicon_path; ?>/apple-icon-60x60.png" sizes="60x60">
    <link rel="apple-touch-icon" href="<?php echo $favicon_path; ?>/apple-icon-72x72.png" sizes="72x72">
    <link rel="apple-touch-icon" href="<?php echo $favicon_path; ?>/apple-icon-114x114.png" sizes="114x114">
    <link rel="apple-touch-icon" href="<?php echo $favicon_path; ?>/apple-icon-120x120.png" sizes="120x120">
    <link rel="apple-touch-icon" href="<?php echo $favicon_path; ?>/apple-icon-144x144.png" sizes="144x144">
    <link rel="apple-touch-icon" href="<?php echo $favicon_path; ?>/apple-icon-152x152.png" sizes="152x152">
    <link rel="apple-touch-icon" href="<?php echo $favicon_path; ?>/apple-icon-180x180.png" sizes="180x180">
    <link rel="icon" type="image/png" href="<?php echo $favicon_path; ?>/android-icon-192x192.png" sizes="192x192">
    <link rel="icon" type="image/png" href="<?php echo $favicon_path; ?>/favicon-32x32.png" sizes="32x32">
    <link rel="icon" type="image/png" href="<?php echo $favicon_path; ?>/favicon-96x96.png" sizes="96x96">
    <link rel="icon" type="image/png" href="<?php echo $favicon_path; ?>/favicon-16x16.png" sizes="16x16">
    <link rel="manifest" href="<?php echo $favicon_path; ?>/manifest.json">
    <meta name="msapplication-TileColor" content="#fff">
    <meta name="msapplication-TileImager" content="ms-icon-144x144">
    <meta name="theme-color" content="#fff">
    <link rel="shortcut icon" type="image/png" href="<?php echo get_template_directory_uri(); ?>/assets/images/icon.png">
    <link rel="shortcut icon" type="image/x-icon" href="<?php echo $favicon_path; ?>/favicon.ico">
    <link rel="icon" type="image/x-icon" href="<?php echo $favicon_path; ?>/favicon.ico">
    <?php
});

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment