Split functions.php into multiple files in WordPress

in Code Write a comment

In this tutorial, I’ll share a quick way on how I split functions.php into multiple files in WordPress.

If you’ve ever built a custom WordPress theme you know that functions.php file can grow big and become difficult to maintain.

You can easily split it into multiple files by category, site section or some other logic.

First of all, let’s create a new folder functions inside the theme folder.

Then split your main functions.php file into separate files and add them to the folder we’ve just created.

Now, it’s time to import those files back to our main functions file. Place the code below to the top of your functions.php file and rename imports accordingly.

$roots_includes = array(
  '/functions/body-class.php',
  '/functions/connections.php'
);

foreach($roots_includes as $file){
  if(!$filepath = locate_template($file)) {
    trigger_error("Error locating `$file` for inclusion!", E_USER_ERROR);
  }

  require_once $filepath;
}
unset($file, $filepath);

If you find this post useful, please let me know in the comments below. 
Cheers,
Renat Galyamov

Want to share this with your friends?
👉renatello.com/wordpress-functions-php

PS: make sure you check other WordPress tutorials, e.g. query posts by date field using WordPress ACFload Google Fonts via functions.php in WordPress and download my plugin that improves your WordPress SEO & Performance.

Write a Comment

Comment