Home >CMS Tutorial >WordPress >All You Need to Know About the New WordPress Site Icon API

All You Need to Know About the New WordPress Site Icon API

William Shakespeare
William ShakespeareOriginal
2025-02-16 08:32:10958browse

WordPress 4.3 introduces the site icon function, allowing users to define a representative icon for the website, which is the website favicon we are familiar with. This article will dive into site icons and their APIs, including their capabilities and filters.

What is the site icon?

If you are familiar with web development, you may already know what the site icon is. It is what WordPress calls favicon. Favicon is an icon that appears on the browser tab when a website is opened, and it is also an icon that appears when you save a web shortcut on your smartphone home screen. After setting up the site icon, it will also appear in your WordPress admin panel.

WordPress 4.3 enables site icon display by default, no additional actions are required. You can use this feature directly as long as you update WordPress to version 4.3.

How to define site icons?

You can define site icons through the theme customizer. There are two ways to access:

  • From the Admin menu, find the Custom option under the Appearance menu.
  • Click the "Custom" button in the admin bar (New in WordPress 4.3).

All You Need to Know About the New WordPress Site Icon API

In the Site Identification option in the Theme Customizer, you can upload or select an image as a site icon. WordPress recommends image sizes of at least 512 pixels wide and high.

All You Need to Know About the New WordPress Site Icon API

After selecting the image, you can crop the image and select the square area you want to be the site icon. Cropping does not modify the original file.

All You Need to Know About the New WordPress Site Icon API

After cropping is completed, click the "Crop Image" button, and WordPress will automatically resize the image. Click the "Save and Publish" button to save the changes.

All You Need to Know About the New WordPress Site Icon API

You can remove or change site icons at any time using the theme customizer.

Get the current site icon

Theme or plugin developers don't need to worry about the basic usage of site icons: After selecting an image, WordPress will automatically display it as favicon.

But if you need to get the current site icon in a theme or plugin, you can use four functions provided by WordPress:

  • has_site_icon(): Check if the site icon is set, returning a boolean value (true or false).
<code class="language-php">if (has_site_icon()) {
    // 已设置站点图标
} else {
    // 未设置站点图标
}</code>
  • get_site_icon_url() and site_icon_url(): Get the site icon URL. get_site_icon_url() You can specify the blog ID and size, and site_icon_url() directly output the URL.
<code class="language-php">if (has_site_icon()) {
    echo '<img alt="Site Icon" src="'%20.%20get_site_icon_url(null,%20200)%20.%20'">';
}</code>
  • wp_site_icon(): Output the necessary meta tags to inform the browser or operating system of the available sizes of favicon.
<code class="language-php">wp_site_icon(); // 通常放在 `` 标签内</code>

Custom site icon size

WordPress generates several sizes of site icons by default. You can use the site_icon_image_sizes and site_icon_meta_tags to customize the size and meta tags.

Summary

WordPress site icon API has simple and practical functions, making it convenient for users and developers to manage website favicon. Remember, this API is mainly used for favicon and is not used for other purposes.

(FAQs part is omitted because it is highly duplicated with existing content and is longer.)

The above is the detailed content of All You Need to Know About the New WordPress Site Icon API. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn