[wp-hackers] Organizing my theme's functions.php file

Micky Hulse mickyhulse.lists at gmail.com
Fri Aug 16 06:37:27 UTC 2013


Hi Brad! Thanks so much for you help/reply, I really appreciate it. :)

On Thu, Aug 15, 2013 at 4:53 PM, Brad Parbs <brad at bradparbs.com> wrote:
> One thing quite a few people do is create a folder `inc/` and put different
> files there, like `scripts.php` that contains script functionality,
> `customizer.php` that holds all the theme customizations stuff, etc. Then
> just include those files in your functions.php.
> You could also move a lot of your functions to a theme-specific plugin or
> multiple plugins, keeping it pretty organized there.

Awesome, thanks for tips! :)

There are some instances where I've moved what would normally be
multiple related functions in `functions.php` into PHP/OOP classes and
suck them into my theme via an `include_once()`. I try to do this for
the bigger chunks of related code, but not usually for the smaller
functions or chunks of code.

Anyway, thanks for listing a few of the alternatives to having
everything in one file, I appreciate the tips. :)

> If you're using prefixed functions `foo_bar_theme_setup(){}`, and that
> prefix is very unique, you don't have to worry about the function_exists,
> unless you plan to override it later. I prefer wrapping every function in
> that, so you can always override them easy later.

Good to know! Thank you for the clarification.

Although, I have to say I'm a little confused ... Looking at:

<https://github.com/WordPress/WordPress/blob/master/wp-content/themes/twentyfourteen/functions.php>

There's not much use of `function_exists()` in WordPress' latest theme.

Not only that, it appears as though most of the `add_action()`'s are
next to their functions, vs. living in the `after_setup_theme`
function. Unless I'm mistaken (I probably am), Justin's article kinda
makes it sound like all function hooks (hook's functions?) should live
in the theme setup function.

So, what's the rule of thumb for when to put stuff inside `after_setup_theme`?

Heck, I'm probably thinking too hard (or, not hard enough) about all this.

My gut is telling me to follow the lead of twentyfoureen (i.e. no
`function_exists()` checks and keep hooks next to their custom
functions vs. move all hooks into `after_setup_theme`).

OTOH, like you say, it can't hurt to use `function_exists()` around everything.

Sorry if I'm rambling.

> Moving all of the hooks into a function is a good way to go, as well.

So many options!

Have you built a theme, or do you know of a theme, that has the
perfect setup (in your opinion) for a `functions.php`? I think I need
to see some good/contemporary examples.

My main concern is that I build the most optimal setup for when it
comes to allowing child themes to override my parent theme's functions
(and yes, this is my first child theme setup, so I'll probably sound
less like a noob once I've had some time to experiment). :D

Thanks again Brad! Much appreciated.


More information about the wp-hackers mailing list