How to Add a Custom Logo to Your WordPress Theme

Pavel T. Dec 23, 2024 Theme Customization
What's the way to put my logo on my website?
What steps should be followed to implement a custom logo upload feature in a WordPress theme using the customizer API?
Andy answered Dec 23, 2024

Adding Custom Logo Support to WordPress

Basic Implementation

First, let's enable custom logo support in your theme by adding this code to your functions.php:

Register theme support for custom logo:

function add_custom_logo_support() {
    add_theme_support('custom-logo', array(
        'height'      => 100,
        'width'       => 400,
        'flex-height' => true,
        'flex-width'  => true,
    ));
}
add_action('after_setup_theme', 'add_custom_logo_support');

Display the logo in your theme (typically in header.php):

function display_custom_logo() {
    if (has_custom_logo()) {
        the_custom_logo();
    } else {
        echo '<a href="' . esc_url(home_url('/')) . '">' . get_bloginfo('name') . '</a>';
    }
}

Best Practices

  1. Image Dimensions

    • Set recommended dimensions in the theme support declaration
    • Use flexible dimensions for better user experience
    • Consider your theme's design when setting size limits
  2. Fallback Strategy

    • Always provide a text fallback when no logo is uploaded
    • Use site title and description as default
  3. Responsive Design

    • Ensure your logo scales properly on different devices
    • Use CSS to control logo appearance:

Add this to your stylesheet:

.custom-logo-link {
    max-width: 100%;
    height: auto;
}
.custom-logo {
    max-width: 100%;
    height: auto;
}

Security Considerations

  1. Always sanitize URLs and escape output:
function secure_logo_display() {
    $custom_logo_id = get_theme_mod('custom_logo');
    $logo = wp_get_attachment_image_src($custom_logo_id, 'full');
    
    if ($logo) {
        echo '<a href="' . esc_url(home_url('/')) . '">';
        echo '<img src="' . esc_url($logo[0]) . '" alt="' . esc_attr(get_bloginfo('name')) . '">';
        echo '</a>';
    }
}

Common Pitfalls to Avoid

  1. Don't forget to add theme support
  2. Don't hardcode logo dimensions in HTML
  3. Don't skip fallback options
  4. Don't forget to handle SVG logos if you plan to support them

Plugin Solutions

If you prefer a plugin-based approach:

  1. Simple Custom Logo
  1. Header Footer Code Manager

Advanced Implementation

Add custom logo size for specific usage:

function custom_logo_setup() {
    add_image_size('custom-logo-size', 300, 100, true);
    
    add_theme_support('custom-logo', array(
        'height'      => 100,
        'width'       => 300,
        'flex-height' => true,
        'flex-width'  => true,
        'size'        => 'custom-logo-size'
    ));
}
add_action('after_setup_theme', 'custom_logo_setup');

Testing Tips

  1. Test logo upload with different image sizes
  2. Verify responsive behavior across devices
  3. Check fallback display works correctly
  4. Validate proper scaling of high-resolution logos

Remember to clear cache and refresh your permalinks after making these changes to avoid any caching issues.