Include widget file in functions.php of child theme

I've created a widget for WP 5.9.3, based on an online tutorial, which I'd got working okay, but it relied on having all the code in the child theme's functions.php, which started looking a bit messy.

So, I've put all the code into a separate file, in the base directory of the child theme, and tried to require it from within the child theme's functions.php.

However, now it doesn't seem to work, or get registered, at all. After several hours of tinkering, it still doesn't seem to be recognized at all. I've looked at a ton of other questions, but I can't find anything which works. The site loads still, but the widget is not visible either on the site or in the Admin area.

Among the things I've tried putting into functions.php, besides hard-coding (last resort) the path, are the following:

1) require_once('my_widget1.php');

2) define('MY_THEME_FOLDER_PATH', trailingslashit(get_template_directory(__FILE__)));
   require_once (MY_THEME_FOLDER_PATH . 'my_widget1.php');
   
3) require_once( get_stylesheet_directory() . '/my_widget1.php');

The actual path to the widget file is:

/var/www/sites/xyz/wp_main/wp-content/themes/mychildtheme/my_widget1.php

I haven't used WP very much, but I'm mystified as to why it doesn't work. I guess I'm missing something obvious, but I'm not sure how to make further progress. Any suggestions and assistance would be much appreciated.

Per the request in the comments, here's the widget code (just minus the output bits). It's basically an example from WPB which I modified, and, as noted, already does work fine when the code is actually inside functions.php. Linux permissions 644.

?php

// Widget Creation
class wpb_widget extends WP_Widget {

function __construct() {
parent::__construct(

// Base ID of your widget
'wpb_widget',

// Widget name will appear in UI
__('WPBeginner Widget', 'wpb_widget_domain'),

// Widget description
array( 'description' = __( 'Sample widget', 'wpb_widget_domain' ), )
);
}

// Creating widget front-end

public function widget( $args, $instance ) {
$title = apply_filters( 'widget_title', $instance['title'] );

// before and after widget arguments are defined by themes
echo $args['before_widget'];
if ( ! empty( $title ) )
echo $args['before_title'] . $title . $args['after_title'];

// This is where you run the code and display the output

$post_id = get_the_ID();

# ... then a bunch of stuff to output 

echo $args['after_widget'];
}

// Widget Backend
public function form( $instance ) {
if ( isset( $instance[ 'title' ] ) ) {
$title = $instance[ 'title' ];
}
else {
$title = __( 'New title', 'wpb_widget_domain' );
}
// Widget admin form
?
p
label for=?php echo $this-get_field_id( 'title' ); ??php _e( 'Title:' ); ?/label
input class=widefat id=?php echo $this-get_field_id( 'title' ); ? name=?php echo $this-get_field_name( 'title' ); ? type=text value=?php echo esc_attr( $title ); ? /
/p
?php
}

// Updating widget replacing old instances with new
public function update( $new_instance, $old_instance ) {
$instance = array();
$instance['title'] = ( ! empty( $new_instance['title'] ) ) ? strip_tags( $new_instance['title'] ) : '';
return $instance;
}

// Class wpb_widget ends here
}


// Register and load the widget
function wpb_load_widget() {
    register_widget( 'wpb_widget' );
}

?

If it's of any relevance, the child theme I'm using is derived from the momentous-lite theme.

Topic child-theme functions widgets Wordpress

Category Web


Okay, so after a couple of days pulling my hair out, I finally realized that I needed to add the following line, after the require_once line in functions.php:

add_action('widgets_init', 'wpb_load_widget');

... and everything then worked perfectly. (where wpb_load_widget is the name of my register-widget function in the widget file itself.)

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.