• Resolved gore.m

    (@gorem)


    Hi,
    in header I’m using the_custom_logo(); and I added support for it, I need change logo via function for certain pages.
    Can anyone help me with that?
    Thanks you

Viewing 6 replies - 1 through 6 (of 6 total)
  • @gorem

    Inside the get_custom_logo() (which the_custom_logo() is a wrapper function for), there is a hook you can use in your functions.php to alter the logo page-by-page. Granted, you haven’t provided very much information so nobody can do that for you, but the filter is ‘get_custom_logo’ and you’d use it like this:

    <?php
    
    add_filter( 'get_custom_logo', 'my_alter_logo_fx', 10, 2 );
    function my_alter_logo_fx( $html, $blog_id ) {
    
        // code here to alter the logo $html depending on the current page, for example make the homepage logo redirect to Google:
        if ( is_front_page() ) {
            $html = sprintf(
    	    '<a href="%1$s" class="custom-logo-link" rel="home">%2$s</a>', 'https://google.com', '<img src="https://mydomain.com/wp-content/uploads/2020/01.logo.png" width="300" height="100" alt="website logo">' );
        }
        return $html;
    
    }
    • This reply was modified 5 years, 1 month ago by Little Package. Reason: missing word in first sentence
    Thread Starter gore.m

    (@gorem)

    @littlepackage Thanks you very much, it works!
    I was trying this function, but it was beyond my knowledge, I couldn’t get it work.
    I have one question, Is it (in your function) everything escaped?
    Thanks you

    @gorem,

    The code I gave you is an example of the direction you could head, and it’s very similar to the function you tried (but I simplified it so you could tell better what’s going on). You’ll probably need to work with a developer to get exactly what you need if it’s all Greek to you.

    I’m not sure what you mean when you want to know if everything is escaped. As it’s written it’s not a function I wouldn’t hesitate to use on my own site.

    Thread Starter gore.m

    (@gorem)

    It was what I exactly needed – to simplified that .-)
    I tried rework your code to my needs, it works, but I would like to ask you to check it if it is right ??
    Thanks you

    add_filter( 'get_custom_logo', 'my_alter_logo_fx', 10, 2 );
    function my_alter_logo_fx( $html, $blog_id ) {
    
    	$url = home_url('/');
    	$whitelogo = home_url( $path = 'images/logo_white.svg');
    	
        if ( is_front_page() ) {
            $html = sprintf(
    	    '<a href="%1$s" class="custom-logo-link" rel="home"><img src="%2$s" class="custom-logo" alt="Logo white" height="28" width="125"></a>', $url , $whitelogo );
        }
        return $html;
    }

    That looks pretty good to me! Good work. Does it work for you? You can always use it to test for other pages, also, not just your chosen “front page,” by using

    if ( is_page( 'slug' ) ) or
    if ( is_page( 'ID' ) ) or
    checking that it ISN’T a page like
    if ( ! is_page( 'slug-of-page-i-do-not-want' ) )

    The only change I’d make is

    add_filter( 'get_custom_logo', 'my_alter_logo_fx', 10, 2 );
    function my_alter_logo_fx( $html, $blog_id ) {
    
        if ( is_front_page() ) {
            // only define these variables if you need to (it's the front page)
    	$url = home_url('/');
    	$whitelogo = home_url( $path = 'images/logo_white.svg');
            $html = sprintf(
    	    '<a href="%1$s" class="custom-logo-link" rel="home"><img src="%2$s" class="custom-logo" alt="Logo white" height="28" width="125"></a>', $url , $whitelogo );
        }
        return $html;
    }
    Thread Starter gore.m

    (@gorem)

    Cool, thanks you very much!

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘How to change logo src via function?’ is closed to new replies.