class WP_Customize_Image_Control {}

Customize Image Control class.

Description

See also

Methods

NameDescription
WP_Customize_Image_Control::add_tabdeprecated
WP_Customize_Image_Control::prepare_controldeprecated
WP_Customize_Image_Control::print_tab_imagedeprecated
WP_Customize_Image_Control::remove_tabdeprecated

Source

class WP_Customize_Image_Control extends WP_Customize_Upload_Control {
	/**
	 * Control type.
	 *
	 * @since 3.4.0
	 * @var string
	 */
	public $type = 'image';

	/**
	 * Media control mime type.
	 *
	 * @since 4.1.0
	 * @var string
	 */
	public $mime_type = 'image';

	/**
	 * @since 3.4.2
	 * @deprecated 4.1.0
	 */
	public function prepare_control() {}

	/**
	 * @since 3.4.0
	 * @deprecated 4.1.0
	 *
	 * @param string $id
	 * @param string $label
	 * @param mixed  $callback
	 */
	public function add_tab( $id, $label, $callback ) {
		_deprecated_function( __METHOD__, '4.1.0' );
	}

	/**
	 * @since 3.4.0
	 * @deprecated 4.1.0
	 *
	 * @param string $id
	 */
	public function remove_tab( $id ) {
		_deprecated_function( __METHOD__, '4.1.0' );
	}

	/**
	 * @since 3.4.0
	 * @deprecated 4.1.0
	 *
	 * @param string $url
	 * @param string $thumbnail_url
	 */
	public function print_tab_image( $url, $thumbnail_url = null ) {
		_deprecated_function( __METHOD__, '4.1.0' );
	}
}

Changelog

VersionDescription
3.4.0Introduced.

User Contributed Notes

  1. Skip to note 5 content

    This class is used with the Theme Customization API to render the custom image control on the Theme Customizer in WordPress 3.4 or newer.

    $wp_customize->add_control(
           new WP_Customize_Image_Control(
               $wp_customize,
               'logo',
               array(
                   'label'      => __( 'Upload a logo', 'theme_name' ),
                   'section'    => 'your_section_id',
                   'settings'   => 'your_setting_id',
                   'context'    => 'your_setting_context' 
               )
           )
       );
  2. Skip to note 6 content
    $wp_customize->add_setting( 'logo', array(
    	'capability'        => 'edit_theme_options',
    	'default'           => '',
    	'sanitize_callback' => 'ic_sanitize_image',
    ) );
    $wp_customize->add_control( new WP_Customize_Image_Control( $wp_customize, 'logo',
    	array(
    		'label'    => __( 'Logo', 'text-domain' ),
    		'section'  => 'general',
    		'settings' => 'logo',
    	)
    ) );

    Callback function

    /**
     * Validation: image
     * Control: text, WP_Customize_Image_Control
     *
     * @uses    wp_check_filetype()        https://developer.wordpress.org/reference/functions/wp_check_filetype/
     * @uses    in_array()                http://php.net/manual/en/function.in-array.php
     */
    function ic_sanitize_image( $file, $setting ) {
    
    	$mimes = array(
    		'jpg|jpeg|jpe' => 'image/jpeg',
    		'gif'          => 'image/gif',
    		'png'          => 'image/png',
    		'bmp'          => 'image/bmp',
    		'tif|tiff'     => 'image/tiff',
    		'ico'          => 'image/x-icon'
    	);
    
    	//check file type from file name
    	$file_ext = wp_check_filetype( $file, $mimes );
    
    	//if file has a valid mime type return it, otherwise return default
    	return ( $file_ext['ext'] ? $file : $setting->default );
    }

You must log in before being able to contribute a note or feedback.