I know this is old but I was looking for an answer to this problem so that I could save a thumbnail rather than use the full size image for a log and couldn't. I have figured it out for my needs by doing the following:
When choosing an image I hook into the customize_sanitize_{$this->id} filter. This gives me the image url in a filter to do what I want with.
Initially I used attachment_url_to_postid
to change the value in the filter callback and then return the new image url from the function, which would then be stored in place of the standard url. However, it turns out that the function that populates the uploader with any previous selection (found in the WP_Customize_Upload_Control
class that the
WP_Customize_Image_Control
inherits from) uses this same function to get the attachment ID, and ultimately render the current selection. This breaks when the URL passed to it is a thumbnail, which is what I'd changed the database value to.
What I did instead was actually save the the value as a wp_option
in the filter callback and just send the standard value right through the function so as not to break it as was happening:
functions.php
function site_logo_save( $value ) {
$id = attachment_url_to_postid($value);
$thumb = wp_get_attachment_image_src( $id );
$url = $thumb[0];
update_option('site_logo', $url);
return $value;
}
add_action('customize_sanitize_site_logo', 'site_logo_save');
Then in my theme file, instead of using get_theme_mod
, I just used get_option
to retrieve the url:
header.php
<div class="site-branding">
<?php if($site_logo = get_option('site_logo')): ?>
<img src="<?php echo get_option('site_logo'); ?>" class="site-logo" alt="<?php bloginfo( 'name' ); ?>">
<?php else: ?>
<h1 class="site-title"><a href="<?php echo esc_url( home_url( '/' ) ); ?>" rel="home"><?php bloginfo( 'name' ); ?></a></h1>
<h2 class="site-description"><?php bloginfo( 'description' ); ?></h2>
<?php endif; ?>
</div><!-- .site-branding -->
<?php
// Add this if we are in the customizer for reseting when no image selected
global $wp_customize;
if ( isset( $wp_customize ) ) : ?>
<div style="display: none;">
<div class="site-branding-no-logo">
<h1 class="site-title"><a href="<?php echo esc_url( home_url( '/' ) ); ?>" rel="home"><?php bloginfo( 'name' ); ?></a></h1>
<h2 class="site-description"><?php bloginfo( 'description' ); ?></h2>
</div>
</div>
<?php endif; ?>
Additionally, because I wanted to use 'transport' => 'postMessage'
for my setting I needed to make an admin-ajax call to get the image thumbnail I was actually going to use for whenever this customizer options was changed in order to reflect this live:
customizer.js
wp.customize( 'site_logo', function( value ) {
value.bind( function( to ) {
if (to == false) {
var orig = $( '.site-branding-no-logo' ).html();
$( '.site-branding' ).html( orig );
} else {
var data = {
action: 'get_actual_site_logo_url'
};
$.post(ajaxurl, data, function(response) {
var $img = $('<img>').attr('src', response);
$( '.site-branding' ).html( $img );
});
}
} );
} );
functions.php
function get_actual_site_logo_url_callback() {
echo get_option('site_logo');
wp_die();
}
add_action( 'wp_ajax_get_actual_site_logo_url', 'get_actual_site_logo_url_callback' );
It's fair to say I had to go along way out of the way to get the functionality I wanted but it seems to be working well!