Question

I'm currently working on a theme that enables the user to upload a logo via the customize theme page.

I want the user to be able to upload an SVG as a logo.

Wordpress does not allow SVG's to be uploaded by default. So I used a snippet of code to enable SVG's in de media uploader.

function cc_mime_types( $mimes ){
    $mimes['svg'] = 'image/svg+xml';
    return $mimes;
}
add_filter( 'upload_mimes', 'cc_mime_types' );

I figured this would also allow me to upload SVG's via the customize theme page (customize.php) .

This doesn't seem to work.

When I upload an svg via the normal media uploader, the file gets uploaded succesfull. When I try to upload an svg via my logo uploader on the customize theme page, nothing happens. Not even an error.

My code to enable logo uploading via the customize theme page:

function themeslug_theme_customizer( $wp_customize ) {
    $wp_customize->add_section( 'themeslug_logo_section' , array(    
    'title'       => __( 'Logo', 'themeslug' ),
    'priority'    => 30,
    'description' => 'Upload a logo to replace the default site name and description     in the header',
) );
$wp_customize->add_setting( 'themeslug_logo' );
$wp_customize->add_control( new WP_Customize_Image_Control( $wp_customize,     'themeslug_logo', array(
    'label'    => __( 'Logo', 'themeslug' ),
    'section'  => 'themeslug_logo_section',
    'settings' => 'themeslug_logo',
) ) );
}
add_action('customize_register', 'themeslug_theme_customizer');

Does anyone know how to enable SVG upload specificaly for the customize theme page?

Was it helpful?

Solution

You'll also need to add "svg" to the list of valid extensions in WP_Customize_Image_Control:

$wp_customize->add_control(
    new WP_Customize_Image_Control(
        $wp_customize,
        'themeslug_logo',
        array(
            'label'      => __( 'Logo', 'themeslug' ),
            'section'    => 'themeslug_logo_section',
            'settings'   => 'themeslug_logo',
            'extensions' => array( 'jpg', 'jpeg', 'gif', 'png', 'svg' ),
        )
    )
);
Licensed under: CC-BY-SA with attribution
Not affiliated with wordpress.stackexchange
scroll top