In the previous post, I spoke briefly about WordPress directories. Specifically, I talked about placing files in an `inc` directory whenever building a theme. This lead to a comment by Richard that I thought was worth covering here:

In the theme folder, sometimes I see people use “lib” to include theme assets and other times I see them use “inc”, as you’ve done here. Is there a reason for one vs the other?

I gave a short response in the comments, but thought that this warranted a longer form explanation to share my perspective, and to hear what you guys have to say, as well.

WordPress Directories

Using `inc` and `lib` aren’t something that I say belong in just themes or belong in just plugins. Instead, they are directories that I’ve used in both themes and plugins.

The `inc` Directory

As my general rule, I use the `inc` directory primary to place collections of functions that are related to core functionality but aren’t necessarily meant to clutter up the primary core of the theme or plugin.

For Themes

For example, if I’m working on a theme and I have a collection of functions for said theme that I use as helper functions, I’d much rather create `inc/helpers.php` than to drop them in `functions.php`. Over time, more helpers are added, the file gets longer, maintenance becomes a pain, and so on.

But in more specific cases, I also use the `inc` directory as a way to store core theme files such as `theme-customizer.php` or `custom-header.php`. This way, these files are focused solely on a single purpose and are easier to maintain over time. Plus, they are self-descriptive.

This keeps `functions.php` lean, and it keeps procedural programming files slightly more organized than having one giant “god-file” by the time of delivery.

For Plugins

In the case of plugins, I generally use object-oriented programming so the `inc` directory is normally used to hold additional classes that I write that are used as part of the core plugin file, but are dependencies.

This means that if I have the core plugin that depends on, say, a custom CSV parser or a serialization / de-serialization class, then these files would reside in `inc`.

The `lib` Directory

In short, the `lib` directory is used for third-party libraries. That is, these are used to make sure that I place code written by another author or team of developers in a place that I can easily retrieve (and attribute – don’t forget! :)) in my project.

The thing about third-party libraries is that they aren’t always PHP-based. Instead, they may be JavaScript based, CSS based, or a combination of all three. In that case, I have to take it case-by-case.

If it’s a third-party JavaScript library such as, say, FitVids, then I’ll have a directory `js/lib/jquery.fitvid.js`’. Similarly, if there’s a CSS libraries that I’m using, like Foundation, then I’ll drop those files in `css/lib/foundation.css`.

Finally, if there is a library that is composed of JavaScript and CSS and/or PHP, then I normally drop them in the `lib` directory in the root of the theme or plugin because most of those files will have dependencies on one another and it’s significantly more painful to try to go through and update all of the relative path references especially when you have to repeat the process when there’s an update.

Also, I know that an alternative to this is using a `vendors` directory. Though I’ve seen the convention and think it’s just as good, I’ve personally just stuck with `lib`.

What About You?

As I mentioned, these are primarily my rules of thumb for organizing my code; however, I know that others have different preferences many of which are likely more efficient or more organized than this.

If that’s the case, I’m all ears as I’m always looking for ways to streamline the development workflow. So if you got one, share it!

Share:

Leave a Reply