jueves, 16 de febrero de 2012

Customising WordPress Custom Headers in Child Themes

I am currently working on the next version of Mimbo Pro (due for release really really soon), and am updating the child themes to work with the new update.

Part of the update is that we (Darren and I) wanted to make the best use of the built in WordPress functionality. One of the elements we wanted to take care of was making sure the header is as flexible as possible. Adding the custom WordPress headers is easy. There's documentation on it on the Codex. Just add a few defines, a couple of callback functions, and you're set.

 define( 'HEADER_TEXTCOLOR', 'ffffff' ); define( 'HEADER_IMAGE', '%s/images/bg_masthead.jpg' ); define( 'HEADER_IMAGE_WIDTH', 960 ); define( 'HEADER_IMAGE_HEIGHT', 108 ); 

The problem is when you want to override these default settings in child themes. You can't just define them again in the child themes functions.php because this will throw up PHP errors.

The solution was actually really simple but it took a bit of thinking to realise all I needed to do is wrap the default properties in a filter so that they could be accessed through the child theme.

 define( 'HEADER_TEXTCOLOR', apply_filters( 'mimbo_header_textcolor', 'ffffff' ) ); define( 'HEADER_IMAGE', apply_filters( 'mimbo_header_image', '%s/images/bg_masthead.jpg' ) ); define( 'HEADER_IMAGE_WIDTH', 960 ); define( 'HEADER_IMAGE_HEIGHT', 108 ); 

Then in the child themes functions.php all I have to do is:

 function dispatch_header_textcolor() { 	return '000000'; }  function dispatch_header_image() { 	return '%s/images/logo.gif'; }  add_filter( 'mimbo_header_textcolor', 'dispatch_header_textcolor' ); add_filter( 'mimbo_header_image', 'dispatch_header_image' ); 

Ok - not very exciting, but I found it very helpful so I'm making a note in case I need it in the future.

No hay comentarios:

Publicar un comentario