Set a Dashboard Logo

This snippet of code will allow you to customize your WordPress dashboard logo.

The two functions below need to be added to your theme’s functions.php file. Note: before doing so, be sure to change “mysite” to your preferred name.

function mysite_customize_register( $wp_customize ) {
    $wp_customize->add_setting( 'mysite_dashlogo', array( 'default' => '' ) );
    $wp_customize->add_control(
        new wp_Customize_Image_Control( $wp_customize, 'dashlogo', array(
            'label'       => __( 'Dashboard Logo', 'mysite' ),
            'section'     => 'title_tagline',
            'settings'    => 'mysite_dashlogo',
            'description' => 'Set a logo for the admin bar',
        ) )
    );
}
add_action( 'customize_register', 'mysite_customize_register' );

function mysite_admin_dashlogo( $wp_admin_bar ) {
    $dashlogo = get_theme_mod( 'mysite_dashlogo');
    if( !empty( $dashlogo ) ) { ?>
        <style type="text/css">
            #wpadminbar #wp-admin-bar-wp-logo > .ab-item .ab-icon:before {
                background: url(<?php echo $dashlogo ?>) no-repeat center center !important;
                color:rgba(0, 0, 0, 0);
            }
            #wpadminbar #wp-admin-bar-wp-logo.hover > .ab-item .ab-icon {
                background-position: 0 0;
            }
        </style>
    <?php }
}
add_action('admin_bar_menu', 'mysite_admin_dashlogo');

Leave a Reply

Your email address will not be published. Required fields are marked *