Keep Your WordPress Functions Organized and Portable

So here is a little tip I thought I would throw out there for everyone.  A way to keep your WordPress functions both organized, and fairly portable.  I actually did this by accident, and only now realized how cool it was!  We all know that functions.php is the place for your functions.  And if you keep adding to it, eventually you can have over a thousand lines of code.  It gets cumbersome and confusing!  And what if you switch themes?  Well check this out.

Hopefully you know how to include other files into your functions.php.  If not, check out this little line of code:

// Include sidebar register code
require_once 'includes/include-sidebars.php';

Simple eh? That’s out of my functions.php.  It simply says to also grab the file include-sidebars.php from my includes folder.  I keep all my extra files in an includes folder.  I just did this because my functions file was huge.  And I like organization.  So I would group similar functions into individual files.  So I have an include-rssfunc file, and includes-options file, an includes-posttype file, you get the idea.  Now I put them into their own folder for my own organization.  The cool thing is, if they are in their own folder, they keep things tidy in the theme, but WordPress still finds these files from the admin end.  So you can edit them from the WordPress theme editor interface.

So one great reason for doing this is for organization of course.  But another side effect I hadn’t considered when I started doing this is portability.  I just started working on the theme here, and wanted to add meta boxes to my theme (we’ll cover that soon).  Well I already had a great block of code, which is kind of lengthy, on my blog.  I could copy and paste the code if I wanted.  But I kept all my meta stuff in my includes-metabox.php file.  So I just took that file from my blog theme, and dragged it to my child theme here and put the proper includes line in my child theme functions.php.

That’s it, done.  I now had all my meta boxes set up on this site.  It made me realize how cool that is.  I can switch themes all I want, and all I have to do is copy the file over to the new theme and add one line of code to functions.php.

So that’s my little tip for you to consider today.  Keep yourself organized, and keep this simple and portable!

2 Replies to “Keep Your WordPress Functions Organized and Portable”

  1. Pingback: traiteur rabat

Leave a Reply