WordPress: Add custom logo support to your theme.

By -

Introduction

Now I don’t want to start of negative but even though I’m not the biggest fan of the WordPress customizer and it will probably be replaced with some kind of Gutenberg implementation in the future. I’m also not a fan of changing client logo’s every time they or a designer decides it needs an update. So we’d better implement this option into the the WordPress admin and be done with it! 🙂

Enable the custom logo customizer

The customizer has this option to upload custom logo’s out of the box, but we do have to enable it before it shows up. To do so we can add the code snippet below to our theme’s functions.php file.

This code is pretty straightforward. All we do is hook into the after_setup_theme action with a function called config_custom_logo. This function uses the add_theme_support function to enable support for the custom logo.

This enables the custom logo feature on the customizer (Appearance > Customize > Site identity) like shown below.

When you click the “Select logo” button you can choose an image from your gallery or upload a new image. When you select an image you are presented with a crop tool for the logo like shown below.

Here you can select a region of the image and click “Crop image”. You will then get a preview of the logo in the Customizer sidebar.

If you are happy with the logo you can click the “Publish” button at the top of the customizer sidebar.

Now for some reason the customizer doesn’t update your logo in the site preview straight away. I haven’t figured out how to fix this yet, but when i do I’ll update this article. If you know of an answer, please share!

Adding the logo to your theme.

Adding the logo to your theme is also pretty simple. Check out the example header code shown below.

Here we have some simple markup for a website header. The only thing you have to do to show the logo is to echo the return value from the get_custom_logo function.

The function call above will generate the following output in you Html code.

All we have to do next is add a little Css to make the logo behave in the page layout. Add the Css code below to your style.css file.

With this Css we give our header a fixed height of 70px. And we make the logo image take 100% of the headers height, with a proportional width.

Of course this Css is very basic and you will have to customize it to meet the needs of you specific theme. But this will give you a nice starting point.

Adding more control over your custom logo.

As you can see implementing a custom logo is pretty simple. But of course we want some more control over the logo. So first of we can add some configurations for the way the logo gets resized and cropped by the user.

Change the config_custom_logo function to reflect the changes below.

Above we add an additional defaults array to the add_theme_support function with the following options.

  • Height: The height we want our logo to be resized to.
  • Width: The width we want our logo to be resized to.
  • Flex-height: Can a user change the height of the crop area?
  • Flex-width: Can a user change the width of the crop area?
  • Header-text: Classes of elements to hide. (Docs)

You will have to play around with these settings to make them fit your theme’s specific logo area.

If you want you can also pass an custom image size, created with the add_image_size function, by passing it’s name for the width and height properties like shown below.

Do note that adding a custom size with the add_image_size function makes WordPress create a copy at that size of every image uploaded, not just your logo. So if your logo has a very specific size don’t use this option because it will create a lot of unused images on disk.

Controlling the output for your custom logo

To customize the way our logo gets added to the page we can create a custom function to replace the get_custom_logo function. Copy the code below to your functions.php file.

Here we check if there is a custom logo uploaded in the admin. if there is we use the get_theme_mod function to get the ID of that logo image, we then pass that to the wp_get_attachement_src function to get that image at a certain size, in this case the ‘full‘ image size.

After that we just echo out some Html markup for the image with a link.

If there is no logo image chosen we echo an H3 tag with a link and the name of the blog/site.

Now that we have this custom function we can use it in our theme like shown below.

Here we replaced the get_custom_logo function with our custom theme_get_custom_logo function.

Our new function will output the Html code like shown below.

Conclusion

As you can see it’s not very hard to add support for custom theme logo’s, and manipulate the output. So now we are done with changing client logo’s 😉

If you want to leave a comment, please do so under the copy of this article on Dev.to so i can get back to you.

Follow me on twitter @Vanaf1979 or on Dev.to @Vanaf1979 to be notified about new articles, and other WordPress development related resources.

Thanks for reading

  • WordPress Development
  • WordPress
  • Tutorial
  • Php