Last active
October 9, 2023 11:04
-
-
Save alexmustin/028f5d577bd2009be5e13a7765745fca to your computer and use it in GitHub Desktop.
Genesis - Custom SVG Logo in WordPress Customizer
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
//* Add support for a Custom Logo | |
add_theme_support( 'custom-logo', array( | |
'width' => 260, | |
'height' => 100, | |
'flex-width' => true, | |
'flex-height' => true, | |
) ); | |
//* Add the custom SVG image, inline, into the Site Title element | |
/* | |
* @param string $title Current markup of title. | |
* @param string $inside Markup inside the title. | |
* @param string $wrap Wrapping element for the title. | |
* | |
* @author @_AlphaBlossom | |
* @author @_neilgee | |
* @author @_JiveDig | |
* @author @_srikat | |
*/ | |
add_filter( 'genesis_seo_title', 'custom_header_inline_logo', 10, 3 ); | |
function custom_header_inline_logo( $title, $inside, $wrap ) { | |
// If the custom logo function above exists, and a custom logo file has been selected, set the Logo Image element inside the wrapping tags. | |
if ( function_exists( 'has_custom_logo' ) && has_custom_logo() ) { | |
$inside = sprintf( '<span class="screen-reader-text">%s</span>%s', esc_html( get_bloginfo( 'name' ) ), get_custom_logo() ); | |
} else { | |
// If no custom logo file selected, wrap around the site name. | |
$inside = sprintf( '<a href="%s">%s</a>', trailingslashit( home_url() ), esc_html( get_bloginfo( 'name' ) ) ); | |
} | |
// Build the title. | |
$title = genesis_markup( array( | |
'open' => sprintf( "<{$wrap} %s>", genesis_attr( 'site-title' ) ), | |
'close' => "</{$wrap}>", | |
'content' => $inside, | |
'context' => 'site-title', | |
'echo' => false, | |
'params' => array( | |
'wrap' => $wrap, | |
), | |
) ); | |
return $title; | |
} | |
//* Output the Custom Logo file | |
//* OPTIONAL: add custom CSS classes to the element | |
//* This is helpful when using the 'SVG Support' plugin, where you may need to supply a special class to enable SVG tag output | |
add_filter( 'get_custom_logo', 'add_custom_logo' ); | |
function add_custom_logo() { | |
$custom_logo_id = get_theme_mod( 'custom_logo' ); | |
$html = sprintf( '<a href="%1$s" class="custom-logo-link" rel="home" itemprop="url">%2$s</a>', | |
esc_url( home_url( '/' ) ), | |
wp_get_attachment_image( $custom_logo_id, 'full', false, array( | |
'class' => 'custom-logo svg', | |
'itemprop' => 'logo', | |
) ) | |
); | |
return $html; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment