0

In wordpress Cherry framework, I am trying to edit template-general.php file functions, but it's different not able to do so. I tried to follow this link, but does not help.

Link for Cherry framework to download file structure is: cherryframework4\lib\functions\template-general.php

I am trying to override below function:

function cherry_get_site_logo( $location = 'header' ) {
    $logo_class = array();

    switch ( $location ) {
        case 'header':
            $type         = cherry_get_option( 'logo-type', 'text' );
            $logo_img_ids = cherry_get_option( 'logo-image-path', false );
            $tag          = is_front_page() ? 'h1' : 'h2';
            $logo_class[] = 'site-title';
            $logo_class[] = $type . '-logo';
            $link_class   = '';
            break;

        case 'footer':
            $type         = cherry_get_option( 'footer-logo-type', 'text' );
            $logo_img_ids = cherry_get_option( 'footer-logo-image-path', false );
            $tag          = 'div';
            $logo_class[] = 'cherry-footer-logo';
            $logo_class[] = $type . '-logo';
            $link_class   = 'footer-logo-link';
            break;

        default:
            $tag          = 'div';
            $logo_class[] = $location . '-logo';
            $link_class   = '';
            break;
    }

    $logo_class = apply_filters( 'cherry_logo_classes', $logo_class, $location );
    $logo_class = array_unique( $logo_class );
    $logo_class = array_map( 'sanitize_html_class', $logo_class );

    if ( 'image' == $type && false != $logo_img_ids ) {

        $images = explode( ',', $logo_img_ids );

        if ( count( $images ) > 1 ) {
            $logo_content = cherry_get_retina_logo( $images );
        } else {

            $img = wp_get_attachment_url( $images[0] );

            $logo_image_format = apply_filters(
                'cherry_logo_image_format',
                '<a href="%1$s" rel="home"><img src="%2$s" alt="%3$s"></a>',
                $location
            );

            $logo_content = sprintf( $logo_image_format, home_url( '/' ), esc_url( $img ), get_bloginfo( 'title' ) );
        }

    } else {
        $logo_content = cherry_get_site_link( $link_class );
    }

    $logo = $logo_content ? sprintf( '<%3$s class="%1$s">%2$s</%3$s>', join( ' ', $logo_class ), $logo_content, $tag ) : '';

    return apply_filters( 'cherry_get_site_logo', $logo, $location );
}

Anyone come across this? Help me find this problem.

7
  • Can you show us what you tried? What didn't work?
    – Tim Malone
    Commented May 10, 2016 at 10:36
  • as i said ,, i followed code.tutsplus.com/tutorials/… this link ... in that link there are three ways of overriding , i tried all those but no effect change from child theme... actually function format i have doesnt match those code in that tutsplus link
    – Arun
    Commented May 10, 2016 at 10:51
  • tutsplus link explained abt 1) if function exists 2) priority override function 3) filter/action/hook removing fns ... these things are not in this function so i dont know how to override
    – Arun
    Commented May 10, 2016 at 11:12
  • there is a filter at the end cherry_get_site_logo why not just hook into that?
    – majick
    Commented May 10, 2016 at 11:27
  • i tried like this function remove_cherry_get_site_logo($location = 'header') { $logo = $logo_content ? sprintf( '<%3$s class="%1$s">%2$s</%3$s>', join( ' ', $logo_class ), $logo_content, $tag ) : ''; return remove_filter('cherry_get_site_logo', $logo, $location ); } add_filter('init','cherry_get_site_logo', $logo, $location );
    – Arun
    Commented May 10, 2016 at 11:56

1 Answer 1

2

Although overriding the function in a child theme is often a good way to go, it looks like this theme doesn't set that up with this particular function (because they're not checking if function_exists() first, before redefining it - which will cause an error).

However, they have given you a way of overriding it with filters.

At the bottom of their function, everything they return is being run through the cherry_get_site_logo filter.

Everytime you see apply_filters() being run in someone else's code, this is something you can hook into :)

To hook into this, you need to add your own filter function. In your function, you can either change their output (eg. through something like str_replace() or preg_replace() to replace parts of it), or you can simply ignore what they've done and provide your own output (which I understand is what you're after).

So in this case, to override the output of their function, you just need to:

add_filter("cherry_get_site_logo", "wpse_226159_site_logo");

function wpse_226159_site_logo($logo, $location){
    $logo = "This is my overriden logo";
    return $logo;
}

This will completely replace their logo output with the text This is my overriden logo. Obviously replace that with whatever you like.

This code goes in your child theme's functions.php file.

6
  • you are great Tim . in this i just wanted to change a variable $tag in switch case "header".. from is_front_page() ? 'h1' : 'h2'; to is_front_page() ? 'h1' : 'h1'; .. how can i change that ? i tried to put only switch case alone in my function but nothing comes in html.. when i put whole code inside my function to replace just $tag, some undefined prob comes.
    – Arun
    Commented May 12, 2016 at 18:25
  • Glad I could help :) Now firstly, you didn't mention that in your original question - it helps if you ask everything relevant at first. Secondly, I can't help with "some undefined prob" - an error is trying to tell you what to fix; it's not a reason to ignore a solution. In your case you need to copy the entire function and change the bit you want to change. If there's an error, work out what that is telling you. Feel free to edit your question and add the details if you need to and I can edit my answer accordingly.
    – Tim Malone
    Commented May 12, 2016 at 19:50
  • Also, the reason they use h1 on the front page and h2 elsewhere is for HTML5 semantics and SEO: there should only ever be one h1 on a page, and on subpages the h1 is usually elsewhere with the title of the page in it. On the home page a common technique is to make the logo the h1 - the name of the site.
    – Tim Malone
    Commented May 12, 2016 at 19:52
  • Tim , the reason i posted generally for overriding is , there are many functions like that so i asked generally and i thought i can work around that $tag variable easily but it isn't so asked you . Secondly i mentioned i got undefined prob because informed you that i tried with these. Basically im a html developer so programitically im lacking.
    – Arun
    Commented May 13, 2016 at 2:02
  • I know they used for seo purpose but i want that logo to be covered with both h1 in all pages .. so i dont know how to override that in switch case . Anyway thanks a lot Tim . Im struggling to override many functions in this theme framework . But patiently getting results one by one :)
    – Arun
    Commented May 13, 2016 at 2:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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