Customizer not saving image settings

I have created an image control in customizer. It displays the field and I can upload / select an image. However, if I publish and leave customizer I can see it doesn’t save the image. All my text / textarea fields work fine. I’m using Underscores as a base theme.

Here’s the field in question in my customizer.php:

...
function minisite_customize_register( $wp_customize ) {
$wp_customize->get_setting( 'blogname' )->transport="postMessage";
$wp_customize->get_setting( 'blogdescription' )->transport="postMessage";
$wp_customize->get_setting( 'header_textcolor' )->transport="postMessage";

// Add Footer Logo Setting
$wp_customize->add_setting( 'footer_logo',
   array(
      'default' => '',
      'transport' => 'refresh',
      'sanitize_callback' => 'absint'
   )
);

$wp_customize->add_control( new WP_Customize_Image_Control( $wp_customize, 'footer_logo',
   array(
      'label' => __( 'Footer Logo' ),
      'description' => esc_html__( 'Choose a footer logo.' ),
      'section' => 'title_tagline',
   )
) );
...

In my footer.php I am using this to test (but I can also see the empty image field when I go back to customizer):

<?php
$footerlogo = get_theme_mod('footer_logo');
if( !empty($footerlogo) )
{
    echo 'yes';
} else {
    echo 'no';
}
?>

I’m following this tutorial to get the image field in customizer:
https://maddisondesigns.com/2017/05/the-wordpress-customizer-a-developers-guide-part-1#imagecontrol

Have I missed something?

1 Answer
1

The problem is in the sanitize_callback = 'absint'.

WP_Customize_Image_Control doesn’t save attachment id so you can’t use absint, If you want to store the ID of the image’s attachment then use WP_Customize_Media_Control, there is also a referance about this here

Leave a Comment