0

How can I set the SRC URL of the custom logo image? I think this can de done best via function.

Normally this is done in "site identity", however I would like to use an externally hosted image (hosted on Amazon S3 if you're wondering). Wordpress only allows custom logo to be an image uploaded to the same installation of Wordpress (as far as I know)

Please note, I want to set the SRC (image source url, not the href).

1

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.