Where to place custom functions?

Straight to the question: It seems that there are only so many places to place custom code: functions.php, plugin, template, shortcode. Having too many plugins slows down your load times, inflating the functions.php file can get unmanageable, same with a template, and shortcodes really depend on the situation.

I have a homepage with a lot of custom code and queries on it: Latest comments, most popular content, most liked content, featured content, custom slider of featured content, newest posts, etc.

Read More

This makes for a cluttered homepage when I’m trying to make edits to the code.

I’ve used shortcodes for a lot of the queries but I’m still left with a cluttered page. My plugins is maxed out (39 active) and my functions is quite full as well. I am packing a lot of custom code into my site but I’m curious as to what your thoughts on the best way to organize all of your custom functions. How do you choose between placing your code into functions.php vs making it a plugin? Are there ways of extending functions.php so that you don’t have to run-up your plugin count and you can better organize your custom functions?

Related posts

Leave a Reply

1 comment

  1. Organize your code by its purpose: If it creates output on the front end and requires changes in your theme’s style sheet – it belongs to the theme. You don’t have to put everything into the functions.php, split he code into several files if it helps.
    For example like this:

    // extra functions just for nav menus
    locate_template( 'php/functions.nav-menu.php', TRUE, TRUE );
    

    If it should survive a theme switch (Custom post types, shortcodes) or is not related to the theme at all – it belongs to a plugin.

    The pure number of plugins is irrelevant: 200 fast plugins are better than one slow plugin.

    Template files are for views – they should not contain function definitions that are hard to find there. And shortcodes are a special API, not places.