I am looking into best practice with Beans on how to include other php files into the functions file. For instance I have lets say 8 widget locations. The code for these take up a lot of space in the functions file. It would be cleaner to take out the code into a seperate widgets.php file and then include it into the functions file.
For whatever reason I am doing this locally I am getting errors when I try:
/* Include widget php file */ include_once ( 'inc/widgets.php' );
include_once( get_stylesheet_directory() . 'inc/widgets.php' );
What is the correct approach?
I checked out various themes and tried a few codes. The following works. Include it inside the functions.php file.
require_once( get_stylesheet_directory() . '/inc/widgets.php' );
Hi Mister, I'm also using this form in function.php:
require_once( get_stylesheet_directory() . '/lib/assets/helpers/translate.php' );
and this one in template page inside function:
echo beans_open_markup( 'main', 'div', array() ); ?><?php include_once( 'lib/uikit-components/plans/plan.inc.php' ); ?><?php echo beans_close_markup( 'main', 'div' );
without any errors.
There are various ways of loading files in PHP and all have there own specificity.
First common answer is that
include_once() will throw warnings and let the processing continue while
require_once() will throw an fatal error if something goes wrong. Awesome info right? lol
What is good to know is that variables are passed when using
include_once() which isn't the case when using
require_once(). This is usually what draws the line and I would suggest to use the includes functions for template files are requires functions for functional files (like in @olivier example in fact).
Also take a look at the WP core template include functions when including template such as
get_template_part() (see [documentation here](https://developer.wordpress.org/reference/functions/get_template_part/())