When to use functions.php or overwrite templates


Hi

I'm building a marketplace website with beans using woocommerce and the wc vendors plugin.

Currently I'm using the functions.php file to drive changes across the whole site and the file is around 1,000 lines long.

Is it better to overwrite the page templates for the woocommerce and wc vendors plugins etc to keep down the size of ther functions.php file?

Or is this not an issue?

many thanks. Jason


Hey Jason,

When it comes to everything related to the frontend output, I would strongly advise to use template files. Here is an answer to a simalar question which should help you understand how to use template files.

Besides the template files, you can/should fragment your code in various files, even organised in directories. Your functions.php should only be the root of your child theme which calls all the other nicely organized files. For example, let's say that you have a set of functions to modify the query, you could add an includes directory and a query.php file in it with all your query functions. Then in your functions.php you would simply include it as follow:

include_once( get_stylesheet_directory() . '/includes/query.php' );

There are lots of tutorials for PHP files structure and how to organise your code, I would suggest to read a bit more about that.

Happy coding,


Thanks Thierry,

So for example if I had lots of modifications to the way posts look etc, would I keep that code in the finctions.php file or in another file somewhere?

Cheers Jason


Organize your files as you wish besides for the template files for which have to follow WP conventions for it to be automatically loaded where application.

If you have a lot of stuff modifying your posts, you may add a post.php file (which is not a WP template file), move all your post related stuff in there and include it the way I explain in my previous answer. That said if all the stuff is modifying your single post view, then you might went to use the WP single-{post-type}.php (in your case single-post.php template file) which will automatically load when a singe post is viewed. If you use the single-post.php template file, don't forget to add beans_load_document() right at the bottom of the file as explained in this article.

It is important to understand the template hierarchy when theming, thus to understand which templates automatically loads. There are a lot of tutorials about it out there for that. Where Beans differs from other themes when using template files is that you don't have to copy and past the entire parent theme template file (monkey work), instead you simply modify what you need using various Beans HTML API functions and call beans_load_document() at the bottom.

Have fun,

Write a reply

Login or register to write a reply, it's free!