Support » Developing with WordPress » Custom Customizer Image Sanitize fails to update image In Customizer

  • I use this custom sanitizer to get the appropriate image size so that anyone using my themes can’t mess up and put an 8k image in the website. The code works, but the problem I have is that once the code sanitizes the image and returns to the customizer the image doesn’t update… the old image just stays displayed and is got a shade of grey over it. What am I missing? If I switch the sanitize function below to say “esc_url” the images work just fine.

    function semperfi_sanitize_image( $input , $setting ) {
        
        $postid = attachment_url_to_postid( $input );
        
        if ( $postid != 0 )
            
            return esc_url( wp_get_attachment_image_src( $postid , $setting->manager->get_control( $setting->id )->input_attrs[img_size] )[0] );
        
        else
            
            return esc_url( $setting->default );
        
    }
    • This topic was modified 10 months, 4 weeks ago by Schwarttzy.
    • This topic was modified 10 months, 4 weeks ago by Schwarttzy.

    The page I need help with: [log in to see the link]

Viewing 2 replies - 1 through 2 (of 2 total)
  • Moderator bcworkz

    (@bcworkz)

    There are a number of possible approaches for getting the preview page to show the latest customizer change. Review the related page in the Theme Handbook.

    What is puzzling is this aspect should already be handled by your theme if you are merely altering the field validation and nothing else. If you are adding new fields, then you must implement the same refresh strategy your theme uses.

    It’s possible your theme has not properly implemented the chosen refresh methods, so you need to understand the content of the linked handbook page in order to evaluate if your theme is handling refreshes properly or not.

    • This reply was modified 10 months, 3 weeks ago by bcworkz.

    Not really sure what the problem is but this works… except when I refresh the customizer it says that there are no images because of the adding 300×300 to the name.

    function semperfi_sanitize_image( $input , $setting )  {
        
        $input = esc_url( $input );
        
        $attrs = $setting->manager->get_control( $setting->id )->input_attrs;
        
        $extension = pathinfo( $input , PATHINFO_EXTENSION );
        
        if ( $input != $setting->default ) {
        
            if ( $extension == 'jpg' ) {
    
                return wp_get_attachment_image_src( attachment_url_to_postid( $input ) , $attrs['img_size'] )[0];
    
            } elseif ( $extension == 'jpeg' ) {
    
                return wp_get_attachment_image_src( attachment_url_to_postid( $input ) , $attrs['img_size'] )[0];
    
            } elseif ( $extension == 'png' ) {
    
                return wp_get_attachment_image_src( attachment_url_to_postid( $input ) , $attrs['img_size'] )[0];
    
            } elseif ( $extension == 'gif' ) {
    
                return $input;
    
            }
            
        } else {
            
            return esc_url( $setting->default );
        
        }
        
    }

    The customizer image needs to a rework in my opinion, could we just add an image size to its options and default to full size if not set?

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Custom Customizer Image Sanitize fails to update image In Customizer’ is closed to new replies.