Skip to content

Instantly share code, notes, and snippets.

@designbuildtest
Created February 15, 2015 06:16
Show Gist options
  • Save designbuildtest/45b9aca250ad2d2ef7de to your computer and use it in GitHub Desktop.
Save designbuildtest/45b9aca250ad2d2ef7de to your computer and use it in GitHub Desktop.
Header Image using WP_Customize_Image_Control
<?php
if ( current_theme_supports( 'onehundred-header-image' ) ) {
function myplugin_header_image_customize_register( $wp_customize ) {
// Pre-selected header images.
$wp_customize->add_setting( 'header_image_selection', array(
'default' => 'default',
'sanitize_callback' => 'myplugin_sanitize_header_image_select_choice',
'transport' => 'postMessage',
) );
$wp_customize->add_control( 'header_image_selection', array(
'label' => __( 'Header Image' ),
'description' => __( 'Choose an image from the options below.' ),
'section' => 'stock_images',
'type' => 'select',
'choices' => myplugin_get_header_image_select_choices(), // found in themes 'skins' folder
'priority' => 5,
) );
// Custom header image.
$wp_customize->add_setting( 'header_image_custom', array(
'sanitize_callback' => 'myplugin_sanitize_header_image_custom',
) );
$wp_customize->add_control( new WP_Customize_Image_Control( $wp_customize, 'header_image_custom', array(
'label' => __( 'Header Image' ),
'section' => 'custom_images',
'priority' => 10,
) ) );
}
add_action( 'customize_register', 'myplugin_header_image_customize_register' );
function myplugin_sanitize_header_image_select_choice( $value ) {
$choices = myplugin_get_header_image_select_choices();
$allowed_choices = array_keys( $choices );
if ( ! in_array( $value, $allowed_choices ) ) {
$value = 'default';
}
return $value;
}
function myplugin_sanitize_header_image_custom( $value ) {
$image_url = '';
$image_id = '';
$filetype = wp_check_filetype( $value );
if ( $filetype["ext"] ) {
$image_url = esc_url_raw( $value );
// Convert Attachment (Image) URL to a Post ID.
$image_id = attachment_url_to_postid( $image_url );
// Save the Post ID.
set_theme_mod( 'header_image_custom_id', $image_id );
}
// If Image is deleted, remove Post ID reference.
if ( ! $image_url ) {
set_theme_mod( 'header_image_custom_id', '' );
}
return $image_url;
}
function myplugin_site_header_background_image_css() {
$template = get_template_directory_uri();
$industry = get_option( 'myplugin_industry', 'construction' );
$filename = get_theme_mod( 'header_image_selection', 'default' );
$header_image_custom = get_theme_mod( 'header_image_custom' );
$header_image_custom_id = get_theme_mod( 'header_image_custom_id' );
global $wp_customize;
echo '<style type="text/css" id="custom-theme-css">';
if ( $header_image_custom && isset( $wp_customize ) ) {
// We need to do a check here as Customizer view will not work properly if we're manipulating using an ID.
echo '.site-header { background-image: url("' . esc_url( $header_image_custom ) . '") } ';
}
elseif ( $header_image_custom_id ) {
$id = absint( $header_image_custom_id );
$image_array = wp_get_attachment_image_src( $id, 'large' );
$image = $image_array[0];
echo '.site-header { background-image: url("' . esc_url( $image ) . '"); } ';
}
else {
echo '.site-header { background-image: url("' . esc_url( $template ) . '/skins/' . esc_attr( $industry ) . '/header-' . esc_attr( $filename ) . '.jpg") } ';
}
echo '</style>';
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment