What is the right way to include custom function in Sage 9?

Hi Guys,

I have a function-admin.php that I would like to use globally in my site.

I was looking in Sage 9 file I couldn’t location the extra.php file.

Their new place where you can include your custom function.php file

can someone show me how please?


1 Like

Feel free to add your own files if it doesn’t fit into one of the existing ones.

1 Like

I’m still little new with Sage Wordpress builder theme so how would go by making my own file like this if may ask?

If you open up functions.php you can see how the files within the src directory are included.

Add your file to the src directory and add it to this array:


As having all my files in the same app/ directory was a bit limited, here’s what I’ve done to load all PHP files from a given directory and its sub-directories.
It means you don’t care about loading order. If you still want to use this but need some files before the custom directory files are loaded, use the array solution provided.

  1. Add a new auto-loaded file to my composer.json
"autoload": {
    "psr-4": {
      "App\\": "app/"
    "files": [
  1. Create the app/lib-autoload.php

glob_recursive needed to load recursively (source)

if ( ! function_exists('glob_recursive'))
    // Does not support flag GLOB_BRACE
    function glob_recursive($pattern, $flags = 0)
        $files = glob($pattern, $flags);
        foreach (glob(dirname($pattern).'/*', GLOB_ONLYDIR|GLOB_NOSORT) as $dir)
            $files = array_merge($files, glob_recursive($dir.'/'.basename($pattern), $flags));
        return $files;

//$files = glob(__DIR__ . '/lib/**/*.php'); //will only load first level sub-dir
$files = glob_recursive(__DIR__ . '/lib/*.php');
if ($files === false) {
    throw new RuntimeException("Failed to glob for function files");
foreach ($files as $file) {
    require_once $file;
unset($file, $files);
  1. all PHP files within app/lib/ directories and sub-directories will be loaded.


I like your autoloader, it’s more robust than mine.

Can I ask what’s the benefit in adding the autoloader to composer.json rather than including it in the “Sage required files” part of functions.php?

Hey Slam,

well I think both include should work.
I used the composer.json include as I already used this on some Laravel projects.

If you succeed to load the lib-autoload.php from the function.php Sage loader, please post a snippet for me to see :slight_smile:

Thanks for your snippet @mtxz.
It saved me some time this afternoon.

I just tested the import of the lib-autoloader from sage official loader and it works perfectly.

Functions now looks like :

array_map( function ( $file ) use ( $sage_error ) {
	$file = "../app/{$file}.php";
	if ( ! locate_template( $file, true, true ) ) {
		$sage_error( sprintf( __( 'Error locating <code>%s</code> for inclusion.', 'sage' ), $file ), 'File not found' );
}, [ 'helpers', 'setup', 'filters', 'admin', 'lib-autoloader' ] ); 

Where ‘lib-autoloader’ is my new file under /app


Just applied this myself; it’s working great. Thanks mtxz!

Thanks mexz :smiley: