Conditional Logic to Check for Site Icon

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="https://wordpress.stackexchange.com/questions/271515/<?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?

2 Answers
2

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    );

Leave a Comment