search
HomeCMS TutorialWordPressHow to Use the WordPress Custom Logo API

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's functions.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(), and has_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.

How to Use the WordPress Custom Logo API

After selecting a logo, the Customizer displays the logo with options to remove or change it.

How to Use the WordPress Custom Logo API

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!

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
What are the advantages of using WordPress over coding a website from scratch?What are the advantages of using WordPress over coding a website from scratch?Apr 25, 2025 am 12:16 AM

WordPressisadvantageousovercodingawebsitefromscratchdueto:1)easeofuseandfasterdevelopment,2)flexibilityandscalability,3)strongcommunitysupport,4)built-inSEOandmarketingtools,5)cost-effectiveness,and6)regularsecurityupdates.Thesefeaturesallowforquicke

What makes WordPress a Content Management System?What makes WordPress a Content Management System?Apr 24, 2025 pm 05:25 PM

WordPressisaCMSduetoitseaseofuse,customization,usermanagement,SEO,andcommunitysupport.1)Itsimplifiescontentmanagementwithanintuitiveinterface.2)Offersextensivecustomizationthroughthemesandplugins.3)Providesrobustuserrolesandpermissions.4)EnhancesSEOa

How to add a comment box to WordPressHow to add a comment box to WordPressApr 20, 2025 pm 12:15 PM

Enable comments on your WordPress website to provide visitors with a platform to participate in discussions and share feedback. To do this, follow these steps: Enable Comments: In the dashboard, navigate to Settings > Discussions, and select the Allow Comments check box. Create a comment form: In the editor, click Add Block and search for the Comments block to add it to the content. Custom Comment Form: Customize comment blocks by setting titles, labels, placeholders, and button text. Save changes: Click Update to save the comment box and add it to the page or article.

How to copy sub-sites from wordpressHow to copy sub-sites from wordpressApr 20, 2025 pm 12:12 PM

How to copy WordPress subsites? Steps: Create a sub-site in the main site. Cloning the sub-site in the main site. Import the clone into the target location. Update the domain name (optional). Separate plugins and themes.

How to write a header of a wordpressHow to write a header of a wordpressApr 20, 2025 pm 12:09 PM

The steps to create a custom header in WordPress are as follows: Edit the theme file "header.php". Add your website name and description. Create a navigation menu. Add a search bar. Save changes and view your custom header.

How to display wordpress commentsHow to display wordpress commentsApr 20, 2025 pm 12:06 PM

Enable comments in WordPress website: 1. Log in to the admin panel, go to "Settings" - "Discussions", and check "Allow comments"; 2. Select a location to display comments; 3. Customize comments; 4. Manage comments, approve, reject or delete; 5. Use <?php comments_template(); ?> tags to display comments; 6. Enable nested comments; 7. Adjust comment shape; 8. Use plugins and verification codes to prevent spam comments; 9. Encourage users to use Gravatar avatar; 10. Create comments to refer to

How to upload source code for wordpressHow to upload source code for wordpressApr 20, 2025 pm 12:03 PM

You can install the FTP plug-in through WordPress, configure the FTP connection, and then upload the source code using the file manager. The steps include: installing the FTP plug-in, configuring the connection, browsing the upload location, uploading files, and checking that the upload is successful.

How to copy wordpress codeHow to copy wordpress codeApr 20, 2025 pm 12:00 PM

How to copy WordPress code? Copy from the admin interface: Log in to the WordPress website, navigate to the destination, select the code and press Ctrl C (Windows)/Command C (Mac) to copy the code. Copy from a file: Connect to the server using SSH or FTP, navigate to the theme or plug-in file, select the code and press Ctrl C (Windows)/Command C (Mac) to copy the code.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Tools

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.