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

J.D. Grimes jdg at codesymphony.co
Fri Aug 16 13:19:53 UTC 2013


Hi, Micky,

I'm not really a theme dev, so bear with me here...

> 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)?


If you want to allow the function to be overridden, there are several other approaches that you could take:

1. You could replace your calls to the function with do_action() or apply_filters(), i.e., create a custom action/filter and hook your function to that.
2. You could call filter(s) or action(s) within the function, allowing other functions to manipulate its output, or even override it completely.

I think doing something like that would be cleaner and possibly more extensible, but that's just me.

> 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'm not sure what the logic behind that would be. The child theme won't be able to remove any of the actions or filters like it normally would if they aren't added until 'after_setup_theme'; it would have to hook into a later action just to manipulate the actions of the parent theme. At least that's what I'm thinking, correct me if I'm wrong.

Good luck!

-J.D.


More information about the wp-hackers mailing list