$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>Is it possible to override this functionclass in a child theme?|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

Is it possible to override this functionclass in a child theme?

matteradmin10PV0评论

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

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.

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/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.

Share Improve this question edited Jun 15, 2020 at 8:21 CommunityBot 1 asked Aug 10, 2014 at 22:14 ChloeChloe 2452 gold badges4 silver badges13 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 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" );' ) );

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
     }
}
Post a comment

comment list (0)

  1. No comments so far