I've developed a lot of PHP code snippets for my website, and I'm not sure where would be the best place to put them in order to reduce site loading times.

If they go in functions.php or in a dedicated plugin, then by default they will be loaded into every page of the site -- not just the pages that need them. Does this have a meaningful impact on load time, or is it inconsequential if the functions are never actually called?

It seems like it would be one step better to use a conditional like if(is_page_template()) { include_once('this-function.php'); } and only load the functions on the pages that actually use them.

I could also include an external php file from the code of the template file itself, or even include the functions directly in the template. Both seem sloppy -- but is it faster?

Is the best method to wrap everything in classes rather than using named functions?

  • 2
    Hi, Welcome to WPSE! Parsing the functions itself won't slow your website, it's the execution that does. As long as those functions aren't used, the overhead is very small.
    – Johansson
    Commented Oct 4, 2018 at 23:28

2 Answers 2


This is a non issue if you use PHP 7.0+ that keeps all files compiled in memory (not 100% accurate but a good enough abstraction), and since all other PHP versions are not supported or are very near their End Of Life, you should upgrade to 7.0+ version if you are not on one.

Regardless, PHP execution is rarely the bottleneck from a performance POV, DB access is usually the bottleneck. It is more important to write your code in a way which is easier to understand and maintain than to look for saving one micro second in page generation.

If you do not have strong opinions of your own, just try to follow how core themes are structured. They do not necessarily have the best structure, but more people are familiar with them and it will be easier to get relevant help either as an advice, or as someone that help coding the theme.


I would wrap my code inside classes, then use an autoload to load the classes only when they are being called by your code.

Here is an example:

// register my autoload function

 * This function is called every time PHP tries to instanciate an undefined class
function my_wp_autoload( $class ) {
    // build the path of the file that holds your class definition
    $filename = dirname( __FILE__ ) . '/include/classes/' . strtolower( $class );

    // if the file exists, include it
    if( file_exists( $filename ) ) {
        include_once( $filename );

The idea behind this is that when you're calling an undefined class in your PHP code, the parser will first execute your autoload function before throwing an exception. It is your last change to include a file that defines the class.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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