3

From what I've read (Check if Favicon is set in Customizer and others), it appears checking to see if a site icon is set in a theme should be easy. It doesn't seem to be working for me. I'd like to have a set of default site icons set in my theme that can be overwritten if a user uploads a site icon. The code I have now is:

  <?php
  if( false === get_option( 'site_icon', false ) ) {
  ?>
  <link rel="apple-touch-icon" sizes="57x57" href="<?php echo get_stylesheet_directory_uri(); ?>/icons/apple-icon-57x57.png">
  <!-- MORE ICONS OUTPUT HERE -->
  <?php
  }
  ?>

This doesn't seem to be working though. Regardless if a site icon is set or not, it will not output. Furthermore, even after deleting an icon from the Customizer section, it stays on the site (even after clearing the site and local cache).

Everything I've read says the site icon should work without any theme support, but it doesn't seem to be working for me. Any insights or something I might be missing?

3

There exists a special function to check if the site icon is set, namely the has_site_icon() function.

So you could try:

add_action( 'wp_head',    'wpse_default_site_icon', 99 );
add_action( 'login_head', 'wpse_default_site_icon', 99 );

function wpse_default_site_icon()
{
    if( ! has_site_icon()  && ! is_customize_preview() )
    {
        // your default icons here
    }
} 

The case when the site icon is set, is already handled by:

add_action( 'wp_head',    'wp_site_icon',  99    );
add_action( 'login_head', 'wp_site_icon',  99    );
  • One question. My theme is adding the site icon automatically to the header. But I developed the theme myself, and didn't include site icon's link anywhere in the templates. Is this automatically done by core – Jack Johansson Jun 28 '17 at 14:25
  • 1
    yes it's added automatically by default via the wp_site_icon() function through the wp_head/login_head actions. If OP is missing the wp_head() then your example would handle both cases . @JackJohansson – birgire Jun 28 '17 at 14:38
  • 1
    Thanks @birgire. I think my issue was I was trying to use the has_site_icon() function in the header.php file. – Andrew Jun 29 '17 at 10:32
2

Let's check if the site icon is set, and then print it:

<?php if (get_option('site_icon')) { ?>
    <link rel="apple-touch-icon" href="<?php echo wp_get_attachment_image_url(get_option('site_icon'),'full') ?>"><?php
} else { ?>
    <link rel="apple-touch-icon" sizes="57x57" href="<?php echo get_stylesheet_directory_uri(); ?>/icons/apple-icon-57x57.png"><?php
} ?>

I'm using this at the moment I'm writing this answer, and it works. This way you can debug to see if there is a site icon in your options or not since this code will output something anyway.

UPDATE

As mentioned in the comment by @birgire, my answer suits the best if you are going to use this code some place other than header, or your theme doesn't have a wp_head function.

If it does, then:

It's added automatically by default via the wp_site_icon() function through the wp_head/login_head actions.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.