4

The functions.php file in the theme I’m working on is becoming too big and hard to manage so I’m thinking about moving some functionality to classes. Is it a bad idea?

Not sure how to do it though: do I need to make them singletons? Do I just call new MyClass in functions.php? What are the best practices for splitting functionality (currently I do it be section, e.g. header functions, content functions, etc.)

  • the first step to try is to split this file in several files which are included in functions.php – mmm Dec 14 '17 at 13:08
  • @mmm this is already done. Those several files are also becoming to big though... – Runnick Dec 14 '17 at 13:17
2

This code https://github.com/chriscoyier/css-tricks-functionality-plugin does exactly what you've described with regard to classes. Although the associated article https://css-tricks.com/wordpress-functionality-plugins/ is about removing non-theme specific functionality out of functions.php to a separate plugin it will give you an insight into the code.

HOWEVER:

PHP includes do have a performance overhead. I have also read (older versions of PHP) that classes add a performance overhead, although I beleive this is not the case with PHP 7.

I am unsure whether you are i. modifying a theme for use on your site, or ii.developing a new Theme.

If just for your site. I improve maintainability and reduce the size of my child functions.php by limiting it to theme specific functions only; other site functionality is contained in a separate plugin as explained in article above (but just the one file not lots of classes and includes). This file is also very large; but I've also commented in an index at the start and prominent section headings and do not have a problem maintaining it.

If you are developing and publishing new theme: bear in mind many users who are happy to use/modify functions do not have the same confidence with object oriented stuff and it may put them off using/childing your theme. Additionally, if they're like me, I find it much easier and less irritating to refer to one large properly commented and sectioned file than have to use functions.php as a look up index to see what other files I need then go away and open to copy, edit or extend.

  • I definitely agree on moving non-theme functionality to plugin. As for functions I understand your point, but I can’t find the way to section functions.php it properly as it appears there are too many levels and just too many lines. – Runnick Dec 15 '17 at 15:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.