skip to Main Content

I’m using Astra theme on WordPress and I’m trying to replace the site logo with an ACF field entry. Having really big difficulties setting the logo at all using Astra’s astra_replace_header_logo filter. Trying to start with baby steps and replace the logo with a hardcoded url or attachment ID, but neither seem to be working.

function custom_astra_replace_header_logo($image) { 
$custom_logo_nav_id = get_field('logo_nav', 'options', false); 
if($custom_logo_nav_id) { 

$image[0] = '/wp-content/uploads/2021/05/new-logo.png'; 
return $image; 
} 
} 

add_filter( 'astra_replace_header_logo', 'custom_astra_replace_header_logo' );

This doesn’t seem to have any effect all. I also tried using the attachment ID together with the size. Astra’s function suggests returning url should be fine though (https://github.com/brainstormforce/astra/blob/master/inc/extras.php#L260).

Interesingly if I set the $image to be something odd, like just the attachment ID $image = 5412 ; I get a broken image on front-end with src(unknown)

Really don’t understand why this isn’t working. Anyone have any experience with this? Any help greatly appreciated, thanks.

2

Answers


  1. Chosen as BEST ANSWER

    Figured out what was wrong. Plugin called WP Offload Media which I use to upload media files to AWS automatically, was for some reason rewriting the URL for the logo and it was always using the original Astra site logo. Even though the function was working. The function works as expected when I disabled Offload Media. Now just need to figure out why that is doing what it's doing :) This was the function I used in the end:

    function custom_astra_replace_header_logo( $image ) { 
    $custom_logo_nav_id = get_field('logo_nav', 'options'); 
        if($custom_logo_nav_id == true){ 
            
            $image[0] = $custom_logo_nav_id; 
            return $image; 
        } 
    
        return $image; 
    } 
    
    add_filter( 'astra_replace_header_logo', 'custom_astra_replace_header_logo' );
    
    

  2. According to the source for that function, it ultimately calls wp_get_attachment_image_src

        function astra_replace_header_logo( $image, $attachment_id, $size, $icon ) {
    
            $custom_logo_id = get_theme_mod( 'custom_logo' );
    
            if ( ! is_customize_preview() && $custom_logo_id == $attachment_id && 'full' == $size ) {
    
                $data = wp_get_attachment_image_src( $attachment_id, 'ast-logo-size' );
    
                if ( false != $data ) {
                    $image = $data;
                }
            }
    
            return apply_filters( 'astra_replace_header_logo', $image );
        }
    

    The function returns false other failure, or an array of:

    [
       $url,       // Image URL,
       $width,     // Image width
       $height,    // Image height
       $cropped,   // Was the image cropped
    ]
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search