ThemeShaper Forums » Thematic

[closed]

How to Keep Organized when Functions.php gets uber long

(6 posts)
  • Started 3 years ago by helgatheviking
  • Latest reply from middlesister
  • This topic is not a support question
  1. when you start doing a LOT of customization in your child theme, you can end up with a monster-sized functions.php that is difficult to navigate. So instead of going crazy looking for functions, I split them up into their own files, put them in a special folder, and then just require them in unctions.php. best practice? i have no idea, but it works for me and helps me stay organized. here is what i do:

    1. create a folder in my child theme called "functions"

    2. in functions.php create a constant that defines the location of this folder

    define('FUNCTIONS_PATH', STYLESHEETPATH . '/functions/');

    do you need a constant? well not really, but if you start adding a lot of extra function files it is nice... and if you happened to move/rename stuff you only have to change the value of the constant and not try to remember everywhere you've used it.

    3. put your custom functions (in this exmaple i will create a file for menu functions where i typically declare all the menu locations, arguments, etc) in their OWN php file and then store it in your functions folder.

    4. in your functions.php add the following (obviously depending on what you called the file in step 3

    require_once(FUNCTIONS_PATH . 'menu-functions.php'); // Custom menu functions

    you can use this multiple times and just require multiple files. I usually use for menu-functions, my exceprt functions/class, theme-functions (post header/footer type stuff), and all my admin/backend stuff. this helps me sort the functions into groups which keeps me organized and not scanning through the thousands of lines of code that can end up in functions.php if you start to get pretty involved.

    hope this helps... enjoy.

    Posted 3 years ago #
  2. oakview
    Member

    This is a great tip Helga!

    I've read several similar posts you (and others) have put up, and would like to tender the idea of having a "sticky" thread, like the Useful Tools thread, that would contain only posts of this nature.

    No questions, just good posts like this to use in theme development. If there are questions, inquirers could simply reference the "sticky" post in question. Too much work for the forum admins?

    Posted 3 years ago #
  3. This should definitely be a sticky, what a great idea! Messing with functions.php when it gets long can be a nightmare :0

    Posted 3 years ago #
  4. it is probably something i'll add to the wiki once that gets going properly. too many stickies and things start to get lost too. but yeah, i couldn't handle everything in functions.php any more, so now i have a nicely organized functions folder filled w/ files like theme-functions, thematic-functions, widget-functions, etc.

    Posted 3 years ago #
  5. A method I use is to add a commented out index of all my function names at the top of functions.php.

    Copy the function name, hit Ctrl + F, paste it and you can get to where you need to go pretty quickly.

    Posted 3 years ago #
  6. middlesister
    Member

    I do the same, but in a slightly different way. I keep all the add_action and add_filter declarations in the actual functions.php file, but move their corresponding functions to the included file(s).

    require_once(FUNCTIONS_PATH . 'menu-functions.php');
    require_once(FUNCTIONS_PATH . 'posts-functions.php');
    
    /*
    *	actions for menu - in menu-functions.php
    */
    // what the filter does
    add_filter('thematic_filter', 'my_function_name');
    // what the action does
    add_action('thematic_hook','my_function_name');
    
    // if there are childtheme_overrides, i write it here too in a comment
    
    /*
    *	actions for posts - in posts-functions.php
    */
    // what the filter does
    add_filter('thematic_filter','my_function_name');
    // what the action does
    add_action('thematic_hook','my_function_name');
    
    // if there are childtheme_overrides, i write it here too in a comment

    This way, it is easy to see what I have added or changed, and I can easily comment out a filter or action for debugging purposes.

    Posted 3 years ago #

RSS feed for this topic

Topic Closed

This topic has been closed to new replies.