Skip to main content

Organize Your WordPress Functions File

May 2nd, 2016

The functions.php file of any WordPress theme is important, adding features and functionality to your WordPress site. Once you start setting custom image sizes, declaring custom post types, declaring navigation menus, establishing sidebars, enqueueing scripts & styles, etc., the WordPress functions file can get out of control pretty quickly.

It’s incredibly easy to keep a neat & tidy functions.php with PHP includes. Instead of one long functions file, divide your functions up into smaller, more manageable files — I like to store mine in a functions subfolder within the theme — and pull those into your main functions.php with a require_once (or require) call.

Example:

This might be the content of a nav-menus.php within my /functions/ folder within my theme

<?php
register_nav_menus( array(
  'main_menu' => 'Main Menu',
  'footer_menu' => 'Footer Menu',
  'mobile_menu' => 'Mobile Menu'
) );

Then within my functions.php, I would have:

require_once(get_template_directory().'/functions/nav-menus.php');

Ah, sweet, sweet theme organization. Say goodbye to digging through a long WordPress theme functions file.

MORE: WordPress Development

Related Articles