Home>Support>Replacing a function in child theme’s functions.php

Replacing a function in child theme’s functions.php

Notice: This thread is over two years old; the information may be outdated. Please consider creating a new thread if you require free support. If you have an active SiteOrigin Premium license, you can email our premium support desk at [email protected].

Hi all,

In my child theme, I’m trying to have my childfunctions.php file replace some of the code from the Vantage theme’s functions.php file. How would I do that for this particular piece of code :

/**
 * Register widgetized area and update sidebar with default widgets
 *
 * @since vantage 1.0
 */
function vantage_widgets_init() {
	register_sidebar( array(
		'name' => __( 'Sidebar', 'vantage' ),
		'id' => 'sidebar-1',
		'before_widget' => '',
		'after_widget' => '',
		'before_title' => '',
		'after_title' => '',
	) );

	register_sidebar( array(
		'name' => __( 'Footer', 'vantage' ),
		'id' => 'sidebar-footer',
		'before_widget' => '',
		'after_widget' => '',
		'before_title' => '',
		'after_title' => '',
	) );

	register_sidebar( array(
		'name' => __( 'Header', 'vantage' ),
		'id' => 'sidebar-header',
		'before_widget' => '',
		'after_widget' => '',
		'before_title' => '',
		'after_title' => '',
	) );
}
add_action( 'widgets_init', 'vantage_widgets_init' );

I have tried using add_action and remove_action and obviously something’s amiss…

This is our free support forum. Replies can take several days. If you need fast email support, please purchase a SiteOrigin Premium license.

  1. 11 years, 4 days ago Greg Priday
    Hi, I Work Here

    I should have made those functions pluggable, but there’s a work around for this. Disclaimer: I haven’t checked any of the following code.

    function vantage_child_init(){
        remove_action( 'widgets_init', 'vantage_widgets_init' );
    }
    add_action('init', 'vantage_child_init');
    
    function vantage_child_widgets_init() {
       // Your own stuff here
    }
    add_action( 'widgets_init', 'vantage_child_widgets_init' );

    Basically the problem is that the child theme’s functions.php is included before the parent theme’s functions.php. So if you call remove_action in the child theme, it needs to be at a point after the parent theme has added the action. Throwing it in an init function does the trick.

  2. 11 years, 4 days ago Cedric Victor

    And it works! Thank you for the quick response!

  3. 11 years, 4 days ago Greg Priday
    Hi, I Work Here

    Any time Cedric :)

Replies on this thread are closed. Please create a new thread if you have a question, or purchase a SiteOrigin Premium license if you need one-on-one email support.

Get The Most Out of SiteOrigin with SiteOrigin Premium

Find Out More