Additional custom header image

Has anyone seen a way to add a new header_image to a theme? I need to add two configurable images in a theme. It doesn't really matter if it gets added as a new "Header" admin theme menu or if it's added to the existing Apperance->Header menu.

I'm a bit new to plugin development so any help in the right direction is appreciated. I can't really find any hooks or actions to attach to.

Similar to this request

Topic custom-header theme-options Wordpress

Category Web


This has already been answered here before:

If you look at the Twenty Eleven theme and other default themes you can use the featured image as a custom header image if its exactly the same width as the default header.

    <?php
        // Check to see if the header image has been removed
        $header_image = get_header_image();
        if ( $header_image ) :
            // Compatibility with versions of WordPress prior to 3.4.
            if ( function_exists( 'get_custom_header' ) ) {
                /*
                 * We need to figure out what the minimum width should be for our featured image.
                 * This result would be the suggested width if the theme were to implement flexible widths.
                 */
                $header_image_width = get_theme_support( 'custom-header', 'width' );
            } else {
                $header_image_width = HEADER_IMAGE_WIDTH;
            }
            ?>
    <a href="<?php echo esc_url( home_url( '/' ) ); ?>">
        <?php
            /*
             * The header image.
             * Check if this is a post or page, if it has a thumbnail, and if it's a big one
             */
            if ( is_singular() && has_post_thumbnail( $post->ID ) &&
                    ( /* $src, $width, $height */ $image = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), array( $header_image_width, $header_image_width ) ) ) &&
                    $image[1] >= $header_image_width ) :
                // Houston, we have a new header image!
                echo get_the_post_thumbnail( $post->ID, 'post-thumbnail' );
            else :
                // Compatibility with versions of WordPress prior to 3.4.
                if ( function_exists( 'get_custom_header' ) ) {
                    $header_image_width  = get_custom_header()->width;
                    $header_image_height = get_custom_header()->height;
                } else {
                    $header_image_width  = HEADER_IMAGE_WIDTH;
                    $header_image_height = HEADER_IMAGE_HEIGHT;
                }
                ?>
            <img src="<?php header_image(); ?>" width="<?php echo $header_image_width; ?>" height="<?php echo $header_image_height; ?>" alt="" />
        <?php endif; // end check for featured image or standard header ?>
    </a>
    <?php endif; // end check for removed header image ?>

See TwentyEleven to get the idea: it uses register_default_headers() to add new header images. In your plugin or theme just add:

add_action( 'after_setup_theme', 'wpse_42529_add_header_images' );

function wpse_42529_add_header_images()
{
    register_default_headers( 
        array(
            'a_short_name' => array(
            'url' => '%s/images/headers/a_short_name.jpg',
            'thumbnail_url' => '%s/images/headers/a_short_name-thumbnail.jpg',
            'description' => __( 'Wheel', 'twentyeleven' )
        ),
        'another_image' => array(
            'url' => '%s/images/headers/another_image.jpg',
            'thumbnail_url' => '%s/images/headers/another_image-thumbnail.jpg',
            'description' => __( 'Shore', 'twentyeleven' )
        )
        )
    );
}

The %s will be replaced by the stylesheet directory URI. You don't have to use that. You can use plugin_dir_url( __FILE__ ) instead of %s.

You can call register_default_headers() multiple times, it works just like a add_default_headers().

Update

To add a new page for custom images similar to the custom header you should extend the class Custom_Image_Header in a theme options page. But this very class is under reconstruction right now – it is almost impossible to write future proof code based on this. I would wait for WordPress 3.4 and build the code on a more stable base.
Well … I will do that, because I need it probably too.

Another way: copy and modify the current class instead of extending it.

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.