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

Brad Parbs brad at bradparbs.com
Thu Aug 15 23:53:23 UTC 2013


There's a few things you could do to improve the structure of the 
functions.php file.

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.

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.

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

Good luck!

-Brad

> Micky Hulse <mailto:mickyhulse.lists at gmail.com>
> August 15, 2013 6:42 PM
> Howdy,
>
> Here's a "pseudo 10,000 ft. view" of my `functions.php` file:
>
> <https://gist.github.com/mhulse/6245870#file-1-pseudo-functions-before-php>
>
> Long story short, I'm in the process of setting up a child theme for a
> co-worker and I decided it was time to do some house cleaning and make
> my parent theme's functions/hooks/other easier for child themes to
> override.
>
> I found this awesome article out on the net:
>
> "WordPress theme function files"
> <http://justintadlock.com/archives/2010/12/30/wordpress-theme-function-files>
>
> Excellent tips!
>
> With that said, I do have some questions:
>
> 1. For all custom functions that aren't tied into a WP hook, should I
> wrap them with `if ( ! function_exists('some_custom_function')) {
> some_custom_function() { ... } }` (as opposed to wrapping all
> functions in a `function_exists` call)?
>
> 2. Based on Justin's excellent article above, I'm under the impression
> that I should move all of my `add_action()`'s, `remove_action()`'s,
> `add_filter()`'s, `remove_filter()`'s (and similar) into the
> `foo_setup` function (which is hooked into `after_setup_theme`)? In
> other words, should my functions.php file look more like this:
>
> <https://gist.github.com/mhulse/6245870#file-2-pseudo-functions-after-php>
>
> I have to admit, that "pseudo 10,000 ft. view" does look much cleaner
> and organized.
>
> I just wanted to ask the pros before I go making sweeping changes to
> my theme's function.php.
>
> Let me know if I need to clarify my questions better.
>
> Many thanks in advance!
>
> Cheers,
> M
> _______________________________________________
> wp-hackers mailing list
> wp-hackers at lists.automattic.com
> http://lists.automattic.com/mailman/listinfo/wp-hackers


More information about the wp-hackers mailing list