Custom header image

WordPress custom header image API gives blog administrator the ability to control and change the header design (image and font color).
This functionality is very easy to add and use, and upgrades the theme usability significantly.
The interface is added to the Appearance menu.

And looks like this:

The code bits are pretty simple:
Assumin the header part lookis like this:
[cc lang='php']


[/cc]

We need to add 3 different secions to out function.php

  1. Default values definition – text color, background image, image width, image height
  2. The API callback function – functions that alter the header css upon activation. There are two such functions:
    • The function set as header_callback is called when displaying a specific blog page. It is activated by the action_hook wp_head
    • The function set as admin_header_callback is called when displaying administration panels
  3. Callback registration

The definitions code looks like this:
[cc lang='php']
// Set some default values
define('HEADER_TEXTCOLOR', 'fff'); // Default text color
define('HEADER_IMAGE', '%s/default.jpg'); // %s is theme dir uri, set a default image
define('HEADER_IMAGE_WIDTH', 500); // Default image width is actually the div's height
define('HEADER_IMAGE_HEIGHT', 150); // Same for height
?>
[/cc]

Once we’ve set the default values we can add the callback functions:
[cc lang='php']
function header_style() {
// You can change these selectors to match your theme
?>

}

// This function is not relevant unless you want your admin
// header to be changed as well... You can leave it empty
function admin_header_style() {
// This function styles the admin page
?>

}
?>
[/cc]

Now we just need to register the callbacks:
[cc lang="php"]
add_custom_image_header('header_style', 'admin_header_style');
?>
[/cc]

Note that this API is activated by calling wp_head() so if your function does not have that function call in header.php you should add it.
In oredr to add wp_head to your header you need to add the following line just before the body tab:
[cc lang="php"][/cc]

Fix for über-long posts

One of the most painful bugs I’ve encountered in WordPress is the fact that if your post is too long (and by long, I mean a 20-page, 4500-word boring academic article): at some point, WordPress hits some upper limit, and will eat your post. Yep, one moment your text is in your WordPress editor (rich or HTML), and then you hit Save, and the damn thing is all gone. You head over to the revision history, in an attempt to retrieve a previous, shorter version of your excruciatingly long post, restore it, edit it all over again, save, and voila! The f**king thing is gone again, and you start wondering how much the sidewalk might hurt if hurl yourself out the window.

Using wordpress custom fields

WordPress custom fields are a great way to give a wordpress theme more flexibility.

Lately, I had some sites that used a bizarre font for page titles, as well as a side image (not inside the page). The pages all used the same structure, only the content and that image changed.

The solution was using custom fields in order to set both.

In order to do that, in my page.php I’ve replaced the title code from:

[cc lang="php"]

ID,”title”,true);?> ” alt=”" />

[/cc]

Note that the get_post_meta function usually returns an array of custom fields, in this case since we know there is only one field named “title”, I’ve added the last parameter $single=true.