I’d like to improve my code and on a current theme, I’ve extended WordPress with custom post types, metadata, customized user authentication, and a number of other items.

Some of these functions exist in the functions.php file while I’ve put others directly on the template files that they relate to. I have little reason for doing this initially, other than it seemed appropriate because said template file was the only file using the specific function.

But I’d like to know what is considered best practice for the sake of speed and security? What do you do?

2 Answers
2

If it is a custom theme for your site then there would no great speed or security improvements by moving functions.

On the other hand if you want to distribute the theme then I recommend having only the minimum php in the page templates files and having the rest of the functions organized in different files. I like the basic theme set up files of the theme to be in the functions.php and the rest in separate organized files. _s does a good job of this. The reason for moving the functions away from the templates files is that it makes it easier to customize and manage. If you need to duplicate a template then you do not end up duplicating functionality.

I would even place the non-presentational functions in a plugin for easy deactivation and management. See this answer for more information.

Leave a Reply

Your email address will not be published. Required fields are marked *