Publishers of technology books, eBooks, and videos for creative people

Home > Articles > Design > Voices That Matter

How to Create Flexible Header Images in WordPress 3.4

📄 Contents

  1. Enabling Flexible Header Support in Your Theme
  2. Using Flexible Custom Headers
  3. That's It, You're Done!
  • Print
  • + Share This
Break free from header image-size constraints! Jessica Neuman Beck, coauthor of WordPress: Visual QuickStart Guide, Second Edition, shows how to use the new flexible header support introduced with the WordPress 3.4 core update.
From the author of

If your WordPress theme supports custom header images, you already know how easy it is to add a unique image to the header portion of your site. But since the dimensions of those images are set by the theme designer, you've been stuck finding an image that will fit in that spot and still look awesome (see Figure 1). That is, until now!

Figure 1 Some images are visually confusing once they've been cropped (top). Having flexible header-image sizes means that you can choose the best crop size for your image (bottom).

The WordPress 3.4 update allows themes to support flexible header sizes. Theme designers can set up flexible header images with just a few lines of code, and the end user can swap in header images of any size—without needing any hacks or code tweaks.

Interest piqued? Great! I'll walk you through the process of enabling flexible header images in your theme, and then I'll show you how to use this feature to make your site truly unique.

Enabling Flexible Header Support in Your Theme

Roll up your shirt sleeves; this bit requires some code! Don't worry; it's mainly just cut-and-paste. You'll need access to your functions.php and header.php files. If you're not comfortable editing these files, look for a theme that natively supports flexible header images, and skip to the second section of this article.

Let's get started:

  1. Log into your WordPress Dashboard and choose Appearance > Editor.
  2. In the Templates list on the right side of your screen, click Theme Functions (see Figure 2).
  3. Figure 2 You can find your functions.php and header.php files in the sidebar of the built-in theme editor.

  4. Add the following code before the closing ?> tag in your functions.php file:
  5. add_theme_support( 'custom-header', array(
    
          // Flex height
          'flex-height' => true,
          // Recommended height
          'height' => 200,
          // Flex width
          'flex-width' => true,
          // Recommended width
          'width' => 1000,
          // Template header style callback
          'wp-head-callback' => $wphead_cb,
          // Admin header style callback
          'admin-head-callback' => $adminhead_cb,
          // Admin preview style callback
          'admin-preview-callback => $adminpreview_cb
    ) );
  6. Click Update File to save.
  7. Open the header.php file in the editor and find the section of the code that displays your header text or image.
  8. Replace the code displaying your header text or image with the following code:
  9. <img src="<?php header_image(); ?>" height="<?php echo get_custom_header()->height; ?>
    " width="<?php echo get_custom_header()->width; ?>" alt="" />
  10. Click Update File to save.

For more information on the default settings and their options, check out the "Custom Headers" article on the WordPress Codex.

  • + Share This
  • 🔖 Save To Your Account