• Working on a child theme for Twenty Ten, I want to set a different default header image. I managed to register new default images but the default image is defined in a constant:
    define( 'HEADER_IMAGE', '%s/images/headers/path.jpg' );

    It would benefit this theme as a framework template if that would be altered to something like:

    define( 'HEADER_IMAGE', apply_filters( 'twentyten_header_image', '%s/images/headers/path.jpg' ) );

    (similar to filters twentyten_header_image_height and twentyten_header_image_width)

    This way, a child theme can change the default header image with this in its functions.php:

    add_filter( 'twentyten_header_image','mychildtheme_header_image');
    function mychildtheme_header_image() {
    	return get_theme_root_uri() . "/" . basename(dirname(__FILE__)) . '/images/headers/newheader.jpg';
    }

    (tested)

Viewing 9 replies - 1 through 9 (of 9 total)
  • I was just thinking exactly the same thing.:)
    Why don’t we make a ticket on Trac?

    I notice it is not incorporated in the latest version and I cannot find a way around it ;( except defining the constant in child theme functions.php before the parent theme does:

    add_action( 'after_setup_theme', 'my_child_theme_setup_0', 0 );
    function my_child_theme_setup_0 () {
    	define( 'HEADER_IMAGE', get_theme_root_uri() . "/" . basename(dirname(__FILE__)) . '/images/headers/ana-lagragna.jpg' );
    }

    But this WILL result in PHP throwing a notice :

    PHP Notice: Constant a already defined...

    Can anyone suggest a better way ?

    I have a child theme and wanted the default header in an images folder in the child theme, and my headers in a sub folder, if this helps.

    All I did was in the function for ‘after_setup_theme’, added the code this worked with no PHP error?

    define( 'HEADER_IMAGE', get_bloginfo('stylesheet_directory') .'/images/header.png' );
    define( 'HEADER_IMAGE_HEIGHT', apply_filters( 'twentyten_header_image_height', 75 ) );
    
    function jorbin_remove_twenty_ten_headers(){
    	unregister_default_headers( array(
    		'berries',
    		'cherryblossom',
    		'concave',
    		'fern',
    		'forestfloor',
    		'inkwell',
    		'path' ,
    		'sunset')
    		);
    }
    add_action( 'after_setup_theme', 'jorbin_remove_twenty_ten_headers', 11 );
    
    // Add our own custom headers packaged with the child theme. in the child theme template directory 'images/headers/'
    	register_default_headers( cms_theme_headers() );

    This now picks up the default header fine on my website from the local folder, I then use the function from Arron Jorbin to unregister the headers.
    added a function after the ‘after_setup_theme’ to loop through the local /images/headers/ folder and add all images to the array, and register these.

    /* Build the Header Array from the theme headers */
    // No need to code the headers just loop through the folder and return a list
    function cms_theme_headers() {
    	global $themename;
        $list = array();
    	$imagepath = STYLESHEETPATH .'/images/headers/';
    	$imageurl = get_bloginfo('stylesheet_directory');
        $dir_handle = @opendir($imagepath) or die("Unable to open $path");
        while($file = readdir($dir_handle)){
            if($file == "." || $file == ".."){continue;}
            $filename = explode(".",$file);
            $cnt = count($filename); $cnt--; $ext = $filename[$cnt];
            if(strtolower($ext) == ('png' || 'jpg')){
       	 	  if (!strpos($file, '-thumbnail') > 0) {
    				$header = array(
    					'url' => $imageurl .'/images/headers/' .$file,
    					'thumbnail_url' => $imageurl .'/images/headers/' .$filename[0] .'-thumbnail.' .$ext,
    					'description' => __( $filename[0], $themename )
    				);
    				array_push($list, $header);
    		  }
            }
        }
        return $list;
    }

    David

    BTW.

    I had to nest the ‘after_setup_theme’ code to de-register the headers inside the child themes function ‘after_setup_theme’

    That was the only way I could get it to work,

    David

    David, that boils down to the same approach as I use in my child theme… Only I do not use that neat and clever cms_theme_headers function you did to automate register_default_headers. Do you do that so users can throw in new images which will then be auto-detected? But what after a theme update? Won’t these be overwritten again? Or do you use it for your own convenience? But then, that function will run on each server request so does it not create unnecessary load?

    Anyway, what I had so far is

    define('MY_CHILD_THEME_URL', get_theme_root_uri() . "/" . basename(dirname(__FILE__)) );
    
    add_filter( 'twentyten_header_image_width', 'my_child_theme_header_image_width' );
    add_filter( 'twentyten_header_image_height', 'my_child_theme_header_image_height' );
    
    function my_child_theme_header_image_width () {
    	return 548;
    }
    function my_child_theme_header_image_height () {
    	return 40;
    }
    
    add_action( 'after_setup_theme', 'my_child_theme_setup_0', 0 );
    function my_child_theme_setup_0 () {
    	define( 'HEADER_IMAGE', MY_CHILD_THEME_URL . '/images/headers/newimage.jpg' );
    }
    
    add_action( 'after_setup_theme', 'ana_lagragna_theme_setup_99', 99 );
    function ana_lagragna_theme_setup_99 () {
    
    	register_default_headers( array(
    		'newname' => array(
    			'url' => MY_CHILD_THEME_URL . '/images/headers/newimage.jpg',
    			'thumbnail_url' => MY_CHILD_THEME_URL . '/images/headers/newimage-thumbnail.jpg',
    			/* translators: header image description */
    			'description' => __( 'New Name', 'twentyten' )
    		),
    		'anothername' => array(
    			'url' => MY_CHILD_THEME_URL . '/images/headers/anotherimage.jpg',
    			'thumbnail_url' => MY_CHILD_THEME_URL . '/images/headers/anotherimage-thumbnail.jpg',
    			/* translators: header image description */
    			'description' => __( 'Another Name', 'twentyten' )
    		),
    	) );
    	unregister_default_headers( array(
    		'berries',
    		'cherryblossom',
    		'concave',
    		'fern',
    		'forestfloor',
    		'inkwell',
    		'path',
    		'sunset'
    	) );
    }

    but I suppose you are right about the define(‘HEADER_IMAGE’, … ) not having to be inside a after_setup_theme call. It can just be defined nude. And for the deregister to work, you do indeed have to call it AFTER the parent theme has done, so 11 or higher ??

    The only concern I had left is that PHP will throw that Notice error the moment the parent theme will try to define HEADER_IMAGE again. I suppose it depends on your server setup whether you get to see that notice (in log file) or not… At any rate, I suppose – since it is not a Warning or Fatal Error – it will not be a show stopper on any setup.

    Or will it?

    Oh, and I even tested another trick:

    add_filter( 'theme_mod_header_image', 'my_child_theme_header_image' );
    
    function my_child_theme_header_image() {
    	return MY_CHILD_THEME_URL . '/images/headers/header.jpg';
    }

    but that will override ANY selected image so quite useless in combination with allowing a configurable header image ??

    Hi RavanH,
    I am still learning WordPress, and the only messages I see are on screen when I forget to close something ‘}’

    You are correct the function makes the headers dynamic, I always try to use this for stylesheets, headers, social icons etc:, it saves editing to change anything, as I do not include the default header in the headers folder, I can then add seasonal headers or the like in the headers folder.

    I also make all the headers available on each page, so I can set a different header from the folder, on any page without having to upload a ‘feature image’.

    I just define the new height inside the main ‘after_setup_theme’, the code above works for my twenty ten child theme.

    Stylesheet path and url also work ok I would use:

    define('MY_CHILD_THEME_URL', get_bloginfo('stylesheet_directory'))

    For Parent theme location I use:

    $imagepath = TEMPLATEPATH .'/images/headers/';
    $imageurl = get_bloginfo('template_directory');

    For Child theme location I use:

    $imagepath = STYLESHEETPATH .'/images/headers/';
    $imageurl = get_bloginfo('stylesheet_directory');

    David

    I added some code to RavanH’s code to automaticly create a thumbnail for a header image… So no need to create them by hand.

    if (!strpos($file, '-thumbnail') > 0) { // exclude thumbnails from list
                    if (!strpos($file, '-thumbnail')) { // create thumbnail
                        image_resize( $imagepath.$file, 240, 48, true, 'thumbnail');
                    }
                        $header = array(
                            'url' => $imageurl .'/images/headers/' .$file,
                            'thumbnail_url' => $imageurl .'/images/headers/' .$filename[0] .'-thumbnail.' .$ext,
                            'description' => __( $filename[0], $themename )
                        );
                        array_push($list, $header);
                    }
                }

    Hi, would it be possible to set the height of the header to auto?
    I want to let users decide the height themselves.
    Possible?

    Thanks

Viewing 9 replies - 1 through 9 (of 9 total)
  • The topic ‘please add HEADER_IMAGE filter’ is closed to new replies.