Split "functions.php" in single modules?

Community Forums Forums General Discussion Split "functions.php" in single modules?

This topic is: not resolved

Tagged: 

This topic contains 4 replies, has 3 voices, and was last updated by  David Chu 1 year ago.

Viewing 5 posts - 1 through 5 (of 5 total)
  • Author
    Posts
  • #61205

    pix3
    Participant
    Post count: 1

    Hello,
    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 file

    Many thanks in advance!
    pix3

    #61225

    David Chu
    Participant
    Post count: 1364

    Hi,
    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:

    [php]
    include_once( CHILD_DIR . ‘/pix-functions.php’ );
    [/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.

    [php]
    add_action( ‘genesis_init’, ‘simplehooks_init’, 20 );
    [/php]

    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

    #61286

    Bill Murray
    Participant
    Post count: 575

    @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.

    #61323

    pix3
    Participant
    Post count: 1

    Thank 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

    `

    #61364

    David Chu
    Participant
    Post count: 1364

    Great!
    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

Viewing 5 posts - 1 through 5 (of 5 total)

You must be logged in to reply to this topic.