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
-
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
-
Fallback Strategy
- Always provide a text fallback when no logo is uploaded
- Use site title and description as default
-
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
- 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
- Don't forget to add theme support
- Don't hardcode logo dimensions in HTML
- Don't skip fallback options
- Don't forget to handle SVG logos if you plan to support them
Plugin Solutions
If you prefer a plugin-based approach:
-
Simple Custom Logo
-
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
- Test logo upload with different image sizes
- Verify responsive behavior across devices
- Check fallback display works correctly
- Validate proper scaling of high-resolution logos
Remember to clear cache and refresh your permalinks after making these changes to avoid any caching issues.