WordPress has an inbuilt logo support. All we need to do is enable it in our custom theme.
function theme_prefix_setup() {
add_theme_support( 'custom-logo', array(
'height' => 100,
'width' => 400,
'flex-height' => true,
'flex-width' => true,
'header-text' => array( 'site-title', 'site-description' ),
) );
}
add_action( 'after_setup_theme', 'theme_prefix_setup' );
Add the above code to functions.php file of your theme. The 2nd argument is optional. This will show an option in admin panel (Appearance > Customize > Site Identity) to upload logo for your website. Your admin panel work is done.
Now, you need to add the logo in your website. Following are the steps for that:
- Get the custom logo id using get_theme_mod function.
- Using the id, get the attached image i.e. our logo. This will be done using function wp_get_attachment_image_src.
$custom_logo_id = get_theme_mod( 'custom_logo' );
$image = wp_get_attachment_image_src( $custom_logo_id , 'full' );
echo $image[0];
That’s all, your custom logo is now supported in your custom theme.