1

I have a widget located at /child-theme/includes/custom-widget.php.

How do I call register_widget and reference that widget?

I've tried something like the following inside functions.php:

function SER_register_widgets() { 
  register_widget( 'includes/custom-widget.php' );
}
add_action( 'widgets_init', 'SER_register_widgets' );

but this does not work.

Help appreciated.

1

include that file in your functions.php file like

require_once('includes/custom-widgets.php');

then you can call add_action() hook, in your case

add_action('widgets_init', 'SER_register_widgets'); .

the idea is to make that function ( ser_register_widgets ) visible in current php file ( functions.php file in this case )

0

OK, that's not how PHP works... ;)

register_widget function gets exactly one param - widget class... You can't pass a file path as a class name... Of course you have to make sure, that such class exists (so you have to include file with that class).

include_once( 'includes/custom-widget.php' );

function SER_register_widgets() { 
  register_widget( 'My_Custom_Widget_Class' );  // <-- change to real class
}
add_action( 'widgets_init', 'SER_register_widgets' );

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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