Best practices/structure for a lot of custom PHP

Hi there,

I’m new to the framework. We are using trellis+bedrock+sage to develop a wordpress site that also interacts with an API on another site (Rails). I know that the sage theme instructs custom php functions to go in extras.php, but I really don’t want to have a long unwieldy file.

I was wondering if there was an example repo of a site that has more webapp type functionality, and how people set up their custom php structure? Do people include it as part of the theme, or as a separate plugin? For us, the custom functionality would never be separate from the theme.

We will be getting json data from an API. I was wondering if there is a roots way of storing that data in a global scope, so I’m not calling it all the time?


You see where extras.php is added in functions.php? You can add any number of files there, but they should really be theme dependent.

Non-theme dependent code (such as CPTs or general helper classes) can be included as mu-plugins.

You can also add any dependency you like via composer. That way API wrappers and such will be automatically included.


Thanks for this @Foxaii
That makes sense!

Do you also know about the second part of my question. If I’m making a request to an API and getting some json data, how should I store that? Or, what is the best practice in dealing with globally scoped variables in sage?

There’s really nothing Sage specific about this. You would want to look at what WordPress itself offers. Generally you want to cache results for 15 minutes, an hour, whatever. Might want to take a look at the Transients cache API

Avoid globals… check and get the transients cache. Use functions and classes, not global vars.


Perfect, exactly what I was looking for @kalenjohnson