Community Forums › Forums › Archived Forums › General Discussion › Split "functions.php" in single modules?
Tagged: functions.php
- This topic has 4 replies, 3 voices, and was last updated 11 years, 7 months ago by
David Chu.
-
AuthorPosts
-
September 7, 2013 at 6:08 am #61205
pix3
MemberHello,
im new to genesis, and i try to setup the "Metro" theme.
In the past i used the plugin toolbox (http://wordpress.org/plugins/toolbox/) to split my functions.php in single modules, but i have problems to get it to work. Then i tested the plugin "code snippets" (http://wordpress.org/plugins/code-snippets/), sadly same here.It seems that genesis actions for example:
//* Remove the site description remove_action( 'genesis_site_description', 'genesis_seo_site_description' );
are not working outside the "functions.php" of the childtheme?
So my question is:
How can i split the "functions.php" in single modules (reusability)?
This is very important for me, i dont want to have all my code and functions in a single php fileMany thanks in advance!
pix3September 7, 2013 at 10:34 am #61225David Chu
ParticipantHi,
Good question. I like to divvy my functions into various files, too.Using several function files in your theme is pretty easy. You just do includes or requires in the main functions.php file, here's an example for an extra functions file in your child theme directory:
include_once( CHILD_DIR . '/pix-functions.php' );
BUT... I'm guessing that this is not what you want. The other part of your question is complex. I have begun breaking my fave functions out into a plugin. However, if you stick Genesis-specific functions into the plugin, you run into this: your plugins activate earlier in the WordPress sequence than your child theme functions. So those functions will do nothing because Genesis isn't loaded yet.
So in my plugin I only have functions that are not Genesis ones. And I use the above method for Genesis functions.
I remember Bill Erickson bandying about ideas concerning how Genesis child themes load the framework, and you'll see that in the first line of your theme's functions.php. He apparently loads Genesis differently, and I can't recall the exact method or where in the sequence it is. But that would be another route you could take. I suppose you could also examine the code of Genesis plugins doing such things, e.g. Genesis Simple Hooks.
UPDATE: I think I may have an answer for my own question:
Genesis Simple Hooks, which is a plugin, but obviously needs to be in sync with Genesis, uses a Genesis hook to handle this! Essentially, this line will make the plugin "wait" for Genesis to load, and then go on and do its thing.add_action( 'genesis_init', 'simplehooks_init', 20 );
So several ideas for you, but some programming skills needed to implement them... I don't know if those plugins you mentioned could be rigged to handle all this, but maybe they could.
Cheers, Dave
Dave Chu ยท Custom WordPress Developer – likes collaborating with Designers
September 7, 2013 at 5:52 pm #61286Bill Murray
Member@David Chu -
The other part of your question is complex.
Here's a simple solution to what might appear to be a complex problem:
Design your plugin so you add a function to the init action, as in:
add_action(' init' ,'my_custom_function');
That overcomes the loading order where plugins load after a theme's functions.php. There are other action hooks that come after functions.php is loaded. We have many custom plugins that use genesis functions, and all work fine. It's only a problem if you don't take WP core loading order into consideration.
Web: https://wpperform.com or Twitter: @wpperform
We do managed WordPress hosting.
September 8, 2013 at 3:07 am #61323pix3
MemberThank you DavidChu and BillMurray!
yes, the action "init" seems to solve the problem!
And with the numeric parameter in "add_action" we can set the order of loading? This is great.Cheers,
pix3`
September 8, 2013 at 11:31 am #61364David Chu
ParticipantGreat!
It's good to have options. And yes, the number has to do with loading priority, essentially timing, so that can be experimented with if needed.As for me, right now I'll keep my Genesis goodies in the child theme. My plugin functions are the ones that could be used with any WP theme, so it's more important to me that they be portable. And there are enough functions now that I don't want to make it too big. ๐ But if someone were planning an uber-Genesis theme plugin, collecting them all together may be cool. I may decide to do that eventually.
If if I wanted to go all fancy, I bet I could just move the Genesis init include from my child theme to the top of the plugin. Then I wouldn't need to worry so much about timing.
One awareness thing is that if you have a lot of functions in your plugin, you might want to keep them commented out until needed - otherwise, I've found that you may see surprise features in your new theme! ๐
Dave
Dave Chu ยท Custom WordPress Developer – likes collaborating with Designers
-
AuthorPosts
- The forum ‘General Discussion’ is closed to new topics and replies.