Extracting code from functions.php – to external php files


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.


Hi guys,

There are various ways of loading files in PHP and all have there own specificity.

First common answer is that include() and include_once() will throw warnings and let the processing continue while require() and 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() and include_once() which isn't the case when using require() or 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/())

Have fun,

Write a reply

Login or register to write a reply, it's free!