Getting the favicon URL in WordPress using get_site_icon_url

The get_site_icon_url function in WordPress is designed to retrieve the URL of the site’s icon, also known as a favicon. This is the small image that appears next to the website’s title in a browser tab. The function returns the URL as a string, which can then be used in various ways within a WordPress theme or plugin.

By retrieving the URL of the site’s icon, the get_site_icon_url function enables developers to include the site icon in different areas of the website beyond the browser tab. This can contribute to a consistent branding across the website. Furthermore, the function can help in creating custom functionalities that involve the use of the site icon.

It’s important to note that the get_site_icon_url function will return an empty string if no site icon has been set in the WordPress customizer. Therefore, it’s advisable to include a check for this in the code to handle such cases appropriately.

Parameters Accepted by get_site_icon_url Function

The get_site_icon_url function in WordPress is designed to accept three parameters. These parameters are:

  • $size (integer): This parameter is optional and it defines the size of the site icon. By default, its value is set to 512 pixels.
  • $url (string): This is also an optional parameter. It is used as a fallback URL in case a site icon cannot be located.
  • $blog_id (integer): This optional parameter is used to specify the ID of the blog for which the site icon is to be obtained. If no value is provided, it defaults to the current blog.

Return Value of get_site_icon_url Function

The get_site_icon_url function, after execution, returns a string. This string represents the URL of the Site Icon.

If the function does not receive any parameters, it will still execute and return the default values as per its design.

Examples

How to Display the Site Icon URL in WordPress

if( function_exists('get_site_icon_url') ) {
 $site_icon_url = get_site_icon_url();
 if ( $site_icon_url ) {
 echo '<img src="'. esc_url($site_icon_url) .'" alt="site-icon">';
 }
}

This code snippet checks if the function get_site_icon_url exists to prevent any errors if the function is not available. If it exists, it gets the site icon URL and stores it in the variable $site_icon_url. It then checks if the site icon URL is not empty. If it is not empty, it outputs an HTML img tag with the site icon URL as the source.

How to Use get_site_icon_url Function with Size Parameter

if( function_exists('get_site_icon_url') ) {
 $site_icon_url = get_site_icon_url( 64 );
 if ( $site_icon_url ) {
 echo '<img src="'. esc_url($site_icon_url) .'" alt="site-icon">';
 }
}

This code snippet is similar to the previous one, but it includes a size parameter of 64 when calling the get_site_icon_url function. This means that the function will return the site icon URL for an icon of size 64×64 pixels.

Conclusion

The get_site_icon_url function in WordPress is a feature that retrieves the URL of the site’s icon. This icon, also known as a favicon, is typically displayed in a browser’s address bar or next to the site name in a bookmark list. The function’s primary purpose is to allow developers to programmatically access and manipulate the site icon’s URL, thus providing an avenue for further customization and control over a website’s branding and visual identity.

Related WordPress Functions