If you set your logo in the customizer and your theme is using the_custom_logo()
or get_theme_logo()
to display the logo, then you can use the wp_get_attachment_image_attributes
and check if the class is "custom-logo". If it is, then change the src attribute to wherever you want. You'll have to still set the logo in the customizer.
namespace StackExchange\WordPress;
function imageAtts( array $attr, \WP_Post $attachment, $size ) : array {
if( 'custom-logo' === $attr[ 'class' ] ) {
$attr[ 'src' ] = 'https://example.com/logo.jpg';
}
return $attr;
}
\add_filter( 'wp_get_attachment_image_attributes', __NAMESPACE__ . '\imageAtts', 10, 3 );
If you don't want the extra step of setting the logo in the customizer, you can use the get_custom_logo
filter.
namespace StackExchange\WordPress;
function imageAtts( string $html, int $blog_id ) : string {
return sprintf(
'<a href="%1$s" class="custom-logo-link" rel="home" itemprop="url">%2$s</a>',
\esc_url( home_url( '/' ) ),
'https://example.com/logo.jpg'
);
}
\add_filter( 'get_custom_logo', __NAMESPACE__ . '\getCustomLogo', 10, 2 );
Either way, in the code for your theme, use the_custom_logo()
to display the logo.