This article explains how to use the WordPress Custom Logo feature, introduced in WordPress 4.5, to easily change a website's logo. It covers both the user perspective and the developer perspective.
Key Features:
- The Custom Logo feature simplifies logo changes via the Theme Customizer.
- Developers enable this feature by using
add_theme_support('custom-logo')
in their theme'sfunctions.php
file. - Logo size is controlled using
add_image_size()
and specifying the size in the theme support declaration. WordPress maintains the original aspect ratio during resizing. - The
the_custom_logo()
,get_custom_logo()
, andhas_custom_logo()
functions manage logo display and retrieval.
User-Side Customization:
Users can easily change their website's logo through the Theme Customizer (accessible via the Appearance menu in the WordPress dashboard). The "Site Identity" panel contains the "Logo" section, where users can select or upload a new logo. The Customizer provides a live preview of changes before saving.
After selecting a logo, the Customizer displays the logo with options to remove or change it.
Developer-Side Implementation:
Themes must explicitly enable the Custom Logo feature. This is done by adding the following code to the theme's functions.php
file:
function mytheme_setup() { add_theme_support('custom-logo'); } add_action('after_setup_theme', 'mytheme_setup');
Controlling Logo Size:
Developers can control the logo's size using add_image_size()
and specifying the size within the add_theme_support()
function:
add_image_size('mytheme-logo', 160, 90); add_theme_support('custom-logo', array( 'size' => 'mytheme-logo' ));
This ensures the logo fits within the specified dimensions while preserving its aspect ratio.
Displaying the Custom Logo:
The the_custom_logo()
function displays the logo with appropriate HTML. get_custom_logo()
retrieves the HTML code, and has_custom_logo()
checks if a logo is set. Here's an example of how to display the logo or the site name if no logo is set:
<?php the_custom_logo(); ?> <?php if ( ! has_custom_logo() ) : ?> <h1><?php bloginfo( 'name' ); ?></h1> <?php endif; ?>
Alternatively, a more robust approach using function_exists()
for backward compatibility:
function mytheme_custom_logo() { $output = ''; if ( function_exists( 'get_custom_logo' ) ) { $output = get_custom_logo(); } if ( empty( $output ) ) { $output = '<h1 id="a-href-esc-url-home-url-get-bloginfo-name-a"><a href="' . esc_url( home_url( '/' ) ) . '">' . get_bloginfo( 'name' ) . '</a></h1>'; } echo $output; }
This function can then be called in the theme's template files.
Conclusion:
The WordPress Custom Logo API simplifies logo customization for both users and developers, enhancing website personalization without requiring code modifications by the end-user. The provided code snippets and explanations empower developers to easily integrate this feature into their themes.
The above is the detailed content of How to Use the WordPress Custom Logo API. For more information, please follow other related articles on the PHP Chinese website!

WordPresspluginssignificantlyenhanceitsCMScapabilitiesbyofferingcustomizationandfunctionality.1)Over50,000pluginsallowuserstotailortheirsiteforSEO,e-commerce,andsecurity.2)Pluginscanextendcorefeatures,likeaddingcustomposttypes.3)However,theycancausec

Yes, WordPress is very suitable for e-commerce. 1) With the WooCommerce plugin, WordPress can quickly become a fully functional online store. 2) Pay attention to performance optimization and security, and regular updates and use of caches and security plug-ins are the key. 3) WordPress provides a wealth of customization options to improve user experience and significantly optimize SEO.

Do you want to connect your website to Yandex Webmaster Tools? Webmaster tools such as Google Search Console, Bing and Yandex can help you optimize your website, monitor traffic, manage robots.txt, check for website errors, and more. In this article, we will share how to add your WordPress website to the Yandex Webmaster Tool to monitor your search engine traffic. What is Yandex? Yandex is a popular search engine based in Russia, similar to Google and Bing. You can excel in Yandex

Do you need to fix HTTP image upload errors in WordPress? This error can be particularly frustrating when you create content in WordPress. This usually happens when you upload images or other files to your CMS using the built-in WordPress media library. In this article, we will show you how to easily fix HTTP image upload errors in WordPress. What is the reason for HTTP errors during WordPress media uploading? When you try to upload files to Wo using WordPress media uploader

Recently, one of our readers reported that the Add Media button on their WordPress site suddenly stopped working. This classic editor problem does not show any errors or warnings, which makes the user unaware why their "Add Media" button does not work. In this article, we will show you how to easily fix the Add Media button in WordPress that doesn't work. What causes WordPress "Add Media" button to stop working? If you are still using the old classic WordPress editor, the Add Media button allows you to insert images, videos, and more into your blog post.

Do you want to know how to use cookies on your WordPress website? Cookies are useful tools for storing temporary information in users’ browsers. You can use this information to enhance the user experience through personalization and behavioral targeting. In this ultimate guide, we will show you how to set, get, and delete WordPresscookies like a professional. Note: This is an advanced tutorial. It requires you to be proficient in HTML, CSS, WordPress websites and PHP. What are cookies? Cookies are created and stored when users visit websites.

Do you see the "429 too many requests" error on your WordPress website? This error message means that the user is sending too many HTTP requests to the server of your website. This error can be very frustrating because it is difficult to find out what causes the error. In this article, we will show you how to easily fix the "WordPress429TooManyRequests" error. What causes too many requests for WordPress429? The most common cause of the "429TooManyRequests" error is that the user, bot, or script attempts to go to the website

WordPresscanhandlelargewebsiteswithcarefulplanningandoptimization.1)Usecachingtoreduceserverload.2)Optimizeyourdatabaseregularly.3)ImplementaCDNtodistributecontent.4)Vetpluginsandthemestoavoidconflicts.5)ConsidermanagedWordPresshostingforenhancedperf


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

WebStorm Mac version
Useful JavaScript development tools

SublimeText3 Linux new version
SublimeText3 Linux latest version

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Atom editor mac version download
The most popular open source editor

Dreamweaver CS6
Visual web development tools
