The solution, needs a custom control object extending the original image control, and does an SQL query to grab the GUID and associated attachment ID on sanitisation. Not nice, kludgey, but it works
$wp_customize->add_setting( 'customimage', array(
'default' => $default,
'capability' => 'edit_theme_options',
'type' => 'option',
'sanitize_callback' => array( 'ICIT_Customize_Image_Control_AttID', 'attachment_guid_to_id' ),
'sanitize_js_callback' => array( 'ICIT_Customize_Image_Control_AttID', 'attachment_guid_to_id' ),
) );
$wp_customize->add_control( new ICIT_Customize_Image_Control_AttID( $wp_customize, "custom_image_attach_id", array(
'label' => $label,
'section' => "custom_image_attach_id",
'settings' => 'customimage'
) ) );
if ( ! class_exists( 'ICIT_Customize_Image_Control_AttID' ) ) {
class ICIT_Customize_Image_Control_AttID extends WP_Customize_Image_Control {
public $context="custom_image";
public function __construct( $manager, $id, $args ) {
$this->get_url = array( $this, 'get_img_url' );
parent::__construct( $manager, $id, $args );
}
// As our default save deals with attachment ids not urls we needs this.
public function get_img_url( $attachment_id = 0 ) {
if ( is_numeric( $attachment_id ) && wp_attachment_is_image( $attachment_id ) )
list( $image, $x, $y ) = wp_get_attachment_image_src( $attachment_id );
return ! empty( $image ) ? $image : $attachment_id;
}
public function attachment_guid_to_id( $value ) {
global $wpdb;
if ( ! is_numeric( $value ) ) {
$attachment_id = $wpdb->get_var( $wpdb->prepare( "SELECT ID FROM $wpdb->posts WHERE post_type="attachment" AND guid = %s ORDER BY post_date DESC LIMIT 1;", $value ) );
if ( ! is_wp_error( $attachment_id ) && wp_attachment_is_image( $attachment_id ) )
$value = $attachment_id;
}
return $value;
}
}
}