WordPress 4.5 Custom Logo API: Getting the logo URL

WordPress 4.5 is here with a new API for a Custom Logo.

Just by adding an add_theme_support like this:  add_theme_support('custom-logo');
Remember it’s best called in the after_setup_theme hook.

So now you’ll have a logo selector in your themes customiser.  You can then use the following to detect the logo, echo the logo or return the logo as a string:
has_custom_logo()
the_custom_logo()
get_custom_logo()

Great I hear you say.  But what if you want to use the image as a background? get_custom_logo() returns the full <img /> tag.

After a bit of digging I found the image ID is stored as a modification for the theme, and retrieve it with this:
$custom_logo_id = get_theme_mod( 'custom_logo' );

Once you have the ID you can get the url and other info as normal with wp_get_attachment_image_src.
$logo_meta = wp_get_attachment_image_src($custom_logo_id,'full');

You can wrap this all up in a nice little function.

function get_custom_logo_src() {
    if (has_custom_logo()) {
        $custom_logo_id = get_theme_mod( 'custom_logo' );
        $logo_meta = wp_get_attachment_image_src($custom_logo_id,'full');
        return $logo_meta[0];
    } else {
        return false;
    }
}

Hope this helps!

Author: Sable

Site owner. Just me - nothing more, nothing less.

One thought on “WordPress 4.5 Custom Logo API: Getting the logo URL”

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.