I've created a new widget based on one of my parent theme's custom widgets. In the parent theme the widget files are in the /inc and template-parts folders. I created these same folders in my child theme but can't get the widget to show up on the widgets page. However, it works if I add the widget code to the functions.php file.


Now I have another problem: Fatal error: Call to undefined function add_action()

This is the code I'm using to register the widget:

// Register the widget
function myplugin_register_widgets() {     
register_widget( 'Home_Categories' ); 
add_action( 'widgets_init', 'myplugin_register_widgets' );

This is how the original widget is registered:

// Register the widget

add_action( 'widgets_init', create_function( '', 'return register_widget("Codilight_Widget_Block1");'));
  • 1
    sorry, but "I have a bug in my code" without showing the code is just a poor question. – Mark Kaplun Feb 25 '17 at 4:53

If you created a custom widget, created a file by example class.my-widget.php containing the class definition of the custom widget, something like this:

class my_widget extends WP_Widget {

    public function __construct() {
        //logic here

    public function widget($args, $instance) {
        //logic here

    public function form($instance) {
        //logic here

    public function update($new_instance, $old_instance) {
        //logic here

//registering my widget so its available in the back-end
add_action('widgets_init', function() {

and the file its inside a folder in your theme like this:

enter image description here

you have to load that file, to do that you have to add this to your functions.php:

//lets define a constant for the URL to your theme folder
define('YOUR_THEME_FOLDER_PATH', trailingslashit(get_template_directory(__FILE__)));
//lets load the custom widget
require_once (YOUR_THEME_FOLDER_PATH . 'inc/class.my-widget.php');

the inc/ part can be updated to the folder the file is.

  • Instead of define('YOUR_THEME_FOLDER_PATH', trailingslashit(get_template_directory(__FILE__))); could I use define('YOUR_THEME_FOLDER_PATH', trailingslashit(get_stylesheet_directory_uri()));? – jrcollins Feb 25 '17 at 22:25
  • 1
    I dont recommend it, that will return the URL instead of the PATH. – David Lee Feb 25 '17 at 22:31
  • that cant be add_action() its a WordPress core function – David Lee Feb 25 '17 at 22:32
  • update your question with all this info and how are you calling your widget in functions.php – David Lee Feb 25 '17 at 22:44
  • 1
    I got it to work by changing get_template_directory(__FILE__) to get_stylesheet_directory(__FILE__) – jrcollins Feb 26 '17 at 0:44

There is no need to "override" any source file to create your own widgets. The existence of the former widget, assuming it is properly coded, should not be an issue by itself, just adds some clutter to the widgets admin UI.

So just add your own widget, inherit the original widget class if it is helpful, and in case you really don't want the original to stay around, just use the https://codex.wordpress.org/Function_Reference/unregister_widget API

  • It's actually a new widget with different functionality but based on one of the theme's custom widgets. – jrcollins Feb 25 '17 at 21:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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