Replace Parent Functions from Child Functions?

  • Anonymous

    #2001

    Okay,

    I have spent three days trying to figure out how to add little icons to the immediate left of widget titles.

    Check it out…

    My question is this:

    I had to modify one of the original functions in the parent theme. Is it possible to set up a child function to replace a parent function?

    Thanks!

    Admin

    Syahir Hakim

    #18109

    For most functions, you can simply declare a new function in the child theme with the same function name. This applies to all functions which are wrapped in the function_exists() conditional.

    For other functions that are not wrapped in the function_exists() conditional, they are added using action hooks, so you can remove that function by using remove_action( 'hook_name', 'function_name' ) to remove the function. And then define your own function and hook it to the same action hook.

    Anonymous

    #18110

    Syahir, you are clear, concise, and prompt as always.

    The specific function, and it’s a big one, I’m speaking of begins:

    function graphene_widgets_init() {

    if (function_exists( 'register_sidebar' ) ) {

    global $graphene_settings;

    So I could just replace this entire function in my child functions, make the adjustments as needed, and should be good to go? This should “replace” my parent function?

    Admin

    Syahir Hakim

    #18111

    Always try to minimise code replacement as much as possible. The bigger the chunk of code you’re replacing, the higher the chances are that something in that big chunk of code will have changed in the next update.

    If you just want to add an image placeholder to the left of the widget title, you can do something like this:

    /**
    * Modify the widget title to include an image placeholder
    */
    function graphene_modify_widget_title( $sidebar ){
    global $wp_registered_sidebars;

    // If this is not the widget area we want to change, don't do anything
    if ( $sidebar['id'] != 'sidebar-widget-area' )
    return;

    // Modify the title parameter
    $sidebar['before_title'] = $sidebar['before_title'] . '<span class="widget-title-icon"></span>';

    // Pass in the modified parameters to the registered sidebar array
    $wp_registered_sidebars[$sidebar['id']] = $sidebar;
    }
    add_action( 'register_sidebar', 'graphene_modify_widget_title' );

    Note that what I have done above is to include an image placeholder in the form of <span class="widget-title-icon"></span>. Since every widget on the site will have a unique ID, you can just use CSS to give different background images to that image placeholder for different widgets. No need to meddle with changing PHP codes anymore.

    Anonymous

    #18112

    Ok, I’ve gotten that far… now, how do I go about inserting the actual images?

    In my css, I’d use something like #widget-title-icon? and then the background property and all that?

    Admin

    Syahir Hakim

    #18113

    Something like this will do it for your My Blogs widget:

    #categories-5 .widget-title-icon {
    background: url(http://www.joshlobe.com/wp-content/images/widget_categories.png) no-repeat;
    display: block;
    width: 28px;
    height: 28px;
    }

    Anonymous

    #18114

    Well, I don’t know how you do it! I mean, did you just come up with that off the top of your head?? WTH??

    Did you ever see that movie with Keanu Reeves… “Johnny Mneumonic”? That’s what I want to do… I want to jack in and get all the knowledge in your head!

    Thanks a ton! This method also preserves everything during updates. You’re the best!

    Anonymous

    #18115

    Ok, I got excited.

    One more thing please…

    This works great for the sidebar widgets. But it is not working for the footer widgets.

    Would I alter the code you provided? Or do I need to make (translation: you show me) a new function for the footer widgets?

    Admin

    Syahir Hakim

    #18116

    That’s because of this code:

    // If this is not the widget area we want to change, don't do anything
    if ( $sidebar['id'] != 'sidebar-widget-area' )
    return;

    Remove that bit to have it applied to all widget areas.

    Anonymous

    #18117

    Simply amazing. I’m super jealous.

    Thank you very much.

    Thanks also for the comment on my site.

Viewing 10 posts - 1 through 10 (of 15 total)

  • 1
  • 2
  • You must be logged in to reply to this topic.