Adding subdirectory to App directory in Sage 9

Hoping to add a subdirectory to the Sage App directory using Sage 9.

I saw this post. However I am working on Sage 9 based theme and it does not include a bootstrap subdirectory.

How would I accomplish this in Sage 9?

You should be able to copy the “Sage required files” function in resources/functions.php and amend it to add your sub directory and files. Something like this:

/**
 * Sage required files
 *
 * The mapped array determines the code library included in your theme.
 * Add or remove files to the array as needed. Supports child theme overrides.
 */
array_map(function ($file) use ($sage_error) {
    $file = "../app/your-new-directory/{$file}.php";
    if (!locate_template($file, true, true)) {
        $sage_error(sprintf(__('Error locating <code>%s</code> for inclusion.', 'sage'), $file), 'File not found');
    }
}, ['test']);

Screen Shot 2022-06-23 at 10.54.45 AM

Sage 9 uses the same loading technique, it just looks a bit different because Sage 10 uses the collect() helper instead of a simple array:

If you want to load files from a subdirectory, you’d just add those files to the list of files to be loaded, i.e.

['helpers', 'setup', 'filters', 'admin', 'subdir/file.php']
1 Like

Something I think they wanted to do in the other post, and what I’ve done before with Post Types is using a file glob to pull in all files, dynamically from a folder. I’ve done this in the past with Post Types, Directives, and ACF Field definitions.

Most of those are handled in Sage 10 by other things though, but I still used this pattern to load Block Pattern definitions.

        /**
         *  Register block patterns
         */
        foreach (glob(__DIR__ . "/BlockPatterns/*.php") as $filename) {
            register_block_pattern(
                'sage/' . basename($filename, '.php'),
                require $filename
            );
        }

This could be combined with the collect and array map to scan multiple folders and load the contained files.

Thanks for these. However, any time I add a ‘subdir/file’ to the list of files to be loaded, I get a white screen in my local environment. No errors, just a blanks screen. I’ve added file names just fine, but if its ‘subdir/file’, the site goes blank.

Here is what I added to functions.php

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','DataVisuals/make-charts','DataVisuals/config-retriever-traits','DataVisuals/global-chart-config-traits']);

In attempt to debug, I placed a var_dump() at the top of one of the subdir/file files and the var_dump prints on the page but the rest is blank.

Any idea what could be causing this?

Thanks for this. This was a back up solution of mine, but I read somewhere this is a slower solution than relying on namespaces. Perhaps I misunderstood.
Also this requires me to add a file under app that all the subdirectory files can compile to which seems a bit redundant.

I was going to give it another day of trying before resorting to this solution.

Something in one of the files you load is causing the problem. Without knowing the content of those files, I can’t speculate what. This loading pattern shouldn’t do anything itself that would cause a white screen. Checking your server logs/php logs might give you more information.

I don’t think I understand what you mean here. The locate_template function doesn’t use namespaces to load files like the composer autoloader, the files can have namespaces but they aren’t required.

The file glob will take longer than parsing an array of explicitly named files, but that isn’t dynamic, so only the named files will load. I like that I can just add a file to the folder and know it will be included.

I don’t know what you mean by adding files under app that all subdirectory files can compile to either. You will be adding folders, and the glob functions are added to the setup.php file in my themes. I jumped back into a sage 9 theme that I had done this extensively with. You can see the structure here, and the globs for each folder to scan them.

folder structure

   /**
     *  Add custom post types
     */
    foreach (glob(__DIR__ . "/PostTypes/*.php") as $filename) {
        include $filename;
    }

    /**
     *  Add custom ACF fields
     */
    foreach (glob(__DIR__ . "/Fields/*.php") as $filename) {
        include $filename;
    }

    /**
     *  Add custom shortcodes
     */
    foreach (glob(__DIR__ . "/Shortcodes/*.php") as $filename) {
        include $filename;
    }

    /**
     *  Add custom Widgets
     */
    foreach (glob(__DIR__ . "/Widgets/*.php") as $filename) {
        include $filename;
    }

The error logs for that white screen will be really helpful, I am certain that it will point you in the right direction.

Ah I see what you did there. I was having them compile to a separate file under app that would then be loaded as a required theme file – thanks for clarifying. That is cleaner.

And yes, checked the logs and found out it was namespacing issue.