3

Is it possible to override this widget function from a parent theme? I saw this blog, but it dealt with a simpler case.

http://venutip.com/content/right-way-override-theme-functions

parent

class Chocolat_Widget_New_Entrys extends WP_Widget {

  function __construct() {...

  function widget( $args, $instance ) {...
}
add_action( 'widgets_init', create_function( '', 'return register_widget( "Chocolat_Widget_New_Entrys" );' ) );

I attempted to use remove_action('widgets_init','???'); but soon realized I could not get a handle on the function that registered it!

I thought about overriding the function and creating a subclass, but still, it is registered in the parent by the name of the parent class.

I thought about just copying the whole class, but child functions.php is loaded before parents.php.

2 Answers 2

5

You simply need to run your code on a higher priority than what the parent theme is, the default on add_action function is 10 so you can use:

function s157343_unregister_widgets() {
     unregister_widget( 'Chocolat_Widget_New_Entrys' );
}
add_action( 'widgets_init', 's157343_unregister_widgets', 20 );

This will unregister that widget. Of course, you can still create a new class that extends that widget's class to override the methods you want and register a new widget based on that:

class my_Chocolat_Widget_New_Entrys extends Chocolat_Widget_New_Entrys() {
    public function __construct() {}
    public function widget( $args, $instance ) {}
}
add_action( 'widgets_init', create_function( '', 'return register_widget( "my_Chocolat_Widget_New_Entrys" );' ) );
3
  • Wow I didn't know about default priorities! Are you sure you don't mean a lower priority? Wouldn't you want the parent to register first, so you can unregister it? If it was higher priority, wouldn't it run first, before the parent gets a chance to register?
    – Chloe
    Aug 11, 2014 at 2:08
  • I got so far as to create a subclass class Child_Chocolat_Widget_New_Entrys extends Chocolat_Widget_New_Entrys {..., but it is giving me errors: Class 'Chocolat_Widget_New_Entrys' not found. I added require_once to import the parent theme, but then it complains Cannot redeclare chocolat_content_width(). So how can I subclass this in the child theme, if the child file is loaded first?
    – Chloe
    Aug 11, 2014 at 2:22
  • 1
    Ok I was able to create a subclass by putting it in a separate file and requiring the separate file containing the subclass right before registering it. add_action( 'widgets_init', function() { require 'child_chocolat_widget.php'; return register_widget( "Child_Chocolat_Widget_New_Entrys" ); } );
    – Chloe
    Aug 11, 2014 at 3:02
0

So here is the clear example :

In functions.php :

add_action( 'widgets_init', function() { 
    require 'child_chocolat_widget.php'; 
    return register_widget(  "Child_Chocolat_Widget_New_Entrys" ); 
} );

In child_chocolat_widget.php [Custom class file, whatever name you can keep ]

class my_Chocolat_Widget_New_Entrys extends Chocolat_Widget_New_Entrys() {
    public function __construct() {}
    public function widget( $args, $instance ) { //override method
     }
}

Your Answer

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

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