KVDD
KVDD

Reputation: 652

Overriding Parent Theme Functions in Wordpress

I am using the free "responsive" Wordpress theme for my website, and using Child Themes for the first time.

I need to add a div tag underneath the widgets <h3> tag, but the sidebar functions where the tag resides is stored in the parents functions, and I'm having trouble overriding the function in my Child Theme.

This is what I have to try and remove the Parents Sidebar Function, and re-add my own, however it's not removing the sidebar:

The original function to add sidebars looks idential to my function child_responsive_widgets_init(), but it's called responsive_widgets_init() if that helps.

<?php

function child_responsive_widgets_init() {

    register_sidebar( array(
                          'name'          => __( 'Main Sidebar', 'responsive' ),
                          'description'   => __( 'Area 1 - sidebar.php - Displays on Default, Blog, Blog Excerpt page templates', 'responsive' ),
                          'id'            => 'main-sidebar',
                          'before_title'  => '<div class="widget-title"><h3>',
                          'after_title'   => '</h3><em>&nbsp;</em></div>',
                          'before_widget' => '<div id="%1$s" class="widget-wrapper %2$s">',
                          'after_widget'  => '</div>'
                      ) );
}
function remove_parent_widgets() {
    remove_action( 'widgets_init', 'responsive_widgets_init' );
}
add_action('init','remove_parent_widgets');

add_action( 'widgets_init', 'parent_unregister_sidebars' );
function parent_unregister_sidebars() {
    unregister_sidebar( 'main-sidebar' );
}

add_action( 'widgets_init', 'child_responsive_widgets_init' );
?> 

Upvotes: 3

Views: 2043

Answers (2)

brasofilo
brasofilo

Reputation: 26055

Moved from the Question into an answer.

Thanks to brasofilo and his solution I've got the functions file overriding properly. Here's the updated code below if anyone wants to know.

function child_responsive_widgets_init() {
    register_sidebar( array(
          'name'          => __( 'Main Sidebar', 'responsive' ),
          'description'   => __( 'Area 1 - sidebar.php - Displays on Default, Blog, Blog Excerpt page templates', 'responsive' ),
          'id'            => 'main-sidebar',
          'before_title'  => '<div class="widget-title"><h3>',
          'after_title'   => '</h3><em>&nbsp;</em></div>',
          'before_widget' => '<div id="%1$s" class="widget-wrapper %2$s">',    
          'after_widget'  => '</div>'
      ) );
}
add_action('after_setup_theme','remove_parent_widgets');

function remove_parent_widgets() {
    remove_action( 'widgets_init', 'responsive_widgets_init' );
}
add_action( 'after_setup_theme', 'child_responsive_widgets_init' );

Upvotes: 0

brasofilo
brasofilo

Reputation: 26055

We have to wait until functions.php file is loaded, then we can intercept the parent's hook.

Add this to your child's functions.php:

add_action( 'after_setup_theme', 'remove_parent_hook_so_22995302' );

function remove_parent_hook_so_22995302()
{
    remove_action( 'widgets_init', 'responsive_widgets_init' );
}

Upvotes: 5

Related Questions