Giving your users changeable or custom headers in themes

Posted by Andrea under Themes, how to

Built in to WordPress, and also available for WordPress MU, is the Custom header API. This service allows you to quickly add some code to a theme’s fuinctions.php file to make the header area completely customizable. User will be able to upload a header image of their choice on their chosen theme, without affecting other blogs using the same theme.

You may want to try this first on a test site of WPMU.

First, check and see if your theme has a functions.php file included. If it doesn’t, make a blank file called functions.php. If it already has code in it, which it will if the theme is widget enabled, then you can paste this code in right before the last ?> tag.

//make changeable header

define('HEADER_TEXTCOLOR', '');
define('HEADER_IMAGE', 'setta.jpg'); // %s is theme dir uri and image filename
define('HEADER_IMAGE_WIDTH', 985);  // make sure these values match the theme header
define('HEADER_IMAGE_HEIGHT', 200);
define( 'NO_HEADER_TEXT', true );

function atypxmas_admin_header_style() {   // change atypxmas to your theme name
?>

// this part adds the header in the admin area
#headimg {
height: <?php echo HEADER_IMAGE_HEIGHT; ?>px;
width: <?php echo HEADER_IMAGE_WIDTH; ?>px;
}

#headimg h1, #headimg #desc {
display: none;}
}

function header_style() {
?>

#header{
background: url(<?php header_image() ?>) no-repeat;}
}

add_custom_image_header(’header_style’, ‘atypxmas_admin_header_style’);  // change atypxmas to your theme name you used above.

And that’s it! Everything else is handled internally by WordPress. To make things even easier, here’s a copy of a sample functions.php file. Please read the code and change the values accordingly for your specific theme, including the filename.

5 Responses to “Giving your users changeable or custom headers in themes”

  1. IndieLab Says:

    Andrea,

    Another great post:) I will certainly review this code in the coming months as I get back into theming.

  2. Barry Says:

    Ha ha - saved me time and effort yet again. Keep up the good work!

  3. IdaWebCo Says:

    Everything I ever needed to know about changeable headers in themes. Thank you so much!

  4. Philip M. Hofer (Frumph) Says:

    Heya Andrea you missed the if ‘blank’ check so that the words can be hidden when you click hide text, the method you have above won’t allow you to hide text.

    I posted the version with the if ‘blank’ statement here:

    http://www.lunchboxfunnies.com/forum/viewtopic.php?t=1825

    Which will allow you to hide text or show text on both the default and custom header.

  5. Websites tagged "themes" on Postsaver Says:

    [...] - Eternal Love saved by punkblackstar2008-12-27 - A8stract NTW saved by xfrancois2008-12-23 - Giving your users changeable or custom headers in themes saved by ckeithley2008-12-23 - Wow, Wordpress… saved by SSJ3raditz2008-12-21 - Installation Guide [...]

Leave a Reply

*
To prove you're a person (not a spam script), type the security word shown in the picture.
Anti-Spam Image



google.load("language", "1"); var curstate = 0; var hasloaded = 0; function bnc_show_translated() { if (hasloaded == 0) { bnc_lang_callback(); hasloaded = 1; } for (i = 0; i < 5; i++) { var elem = $("bnc_original_" + i); if (elem) { if (curstate) { elem.show(); } else { elem.hide(); } } } for (i = 0; i < 5; i++) { var elem = $("bnc_trans_" + i); if (elem) { if (curstate) { elem.hide(); } else { elem.show(); } } } if (curstate) { $("bnc_trans_state1").show(); $("bnc_trans_state2").hide(); curstate = 0; } else { $("bnc_trans_state1").hide(); $("bnc_trans_state2").show(); curstate = 1; } } function bnc_detect_div(div_id) { var text = document.getElementById(div_id); if (text) { text = text.innerHTML; if (text.length > 0) { google.language.detect(text, function(result) { if (!result.error) { if (result.language != "en") { if (result.confidence > 0.25) { $("bnc_translating").show(); bnc_xlate_div(result.language, div_id, "en"); } } } } ); } } } function bnc_xlate_div(src_lang,div_id,o_lang) { var text = document.getElementById(div_id); if (text) { text = text.innerHTML; google.language.translate(text, src_lang, o_lang, function(result) { var translated = document.getElementById(div_id); if (result.translation) { translated.innerHTML = result.translation; } }); } } function bnc_lang_callback() { bnc_xlate_div("en", "bnc_lang_i_0_65", "en"); bnc_xlate_div("en", "bnc_lang_i_1_65", "en"); bnc_xlate_div("en", "bnc_lang_i_2_65", "en"); bnc_xlate_div("en", "bnc_lang_i_3_65", "en"); bnc_xlate_div("en", "bnc_lang_i_4_65", "en"); bnc_xlate_div("en", "bnc_lang_i_5_65", "en"); bnc_xlate_div("en", "bnc_lang_i_6_65", "en"); bnc_xlate_div("en", "bnc_lang_i_7_65", "en"); bnc_xlate_div("en", "bnc_lang_i_8_65", "en"); } function bnc_startup() { bnc_xlate_div("en", "bnc_translate_info", "en"); bnc_xlate_div("en", "bnc_translate_info2", "en"); bnc_detect_div("bnc_lang_i_0_65");bnc_detect_div("bnc_lang_i_1_65");bnc_detect_div("bnc_lang_i_2_65");bnc_detect_div("bnc_lang_i_3_65");bnc_detect_div("bnc_lang_i_4_65");bnc_detect_div("bnc_lang_i_5_65");bnc_detect_div("bnc_lang_i_6_65");bnc_detect_div("bnc_lang_i_7_65");bnc_detect_div("bnc_lang_i_8_65"); } google.setOnLoadCallback(bnc_startup);
Close
E-mail It
This blog has been fine-tuned with 13 WordPress Tweaks.