Halloween Sale 15% off Ends in See the plans
PHP

Use the Main Site's Icon on Sites Without One

Makes subsites that have no Site Icon of their own show the main site's favicon and app icon, so every browser tab carries your brand.

Don’t have WPIDE yet? Install it on your site

Code

if ( ! is_multisite() ) {
    return;
}

add_filter( 'get_site_icon_url', function ( $url, $size, $blog_id ) {
    $main = get_main_site_id();

    if ( $url || (int) $blog_id === $main || ( ! $blog_id && is_main_site() ) ) {
        return $url;
    }

    $key    = 'wpide_main_icon_' . (int) $size;
    $cached = wp_cache_get( $key, 'wpide' );

    if ( false === $cached ) {
        $icon_id = (int) get_blog_option( $main, 'site_icon' );
        $cached  = '';
        if ( $icon_id ) {
            switch_to_blog( $main );
            $cached = (string) wp_get_attachment_image_url( $icon_id, array( $size, $size ) );
            restore_current_blog();
        }
        wp_cache_set( $key, $cached, 'wpide', HOUR_IN_SECONDS );
    }

    return $cached ? $cached : $url;
}, 10, 3 );

Notes

Sites that upload their own icon keep it.

How to add this snippet

  1. Install WPIDE on your WordPress site (it is free) and open Code Snippets.
  2. Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
  3. It is added inactive: review the code and settings, then switch it on.

Have a snippet worth sharing?

Publish it to the library from WPIDE or right here. Sign in with any WordPress site that runs WPIDE, free or Pro. Public snippets are reviewed by the WPIDE team before they appear.

Which site should get this snippet?

Enter the address of your WordPress site. WPIDE opens there in a new tab with the snippet ready to review and save.

Remembered in this browser only, so you are asked once.

Report this snippet

Tell the WPIDE team what is wrong: broken code, something harmful, spam or copied work.