Thursday, April 24, 2014

Wordpress Notes 3 - Extreme Refactoring

I've been having a look at menus but while I've been doing that, I've been going through this whole "that's dumb" and "how can I make that a whole lot more modular in order to make it more reusable?" buzz. It's become a really good habit of mine to make something functional and then to go over it to try and turn it into something that I could hopefully use elsewhere.

For example, the presentation of the search box in my previous box is highly reliant on css... so why not separate off the css for the searchbox? And while I'm doing imports of css files, I might as well have them in their own place. I was similarly irritated by functions.php. Why dump EVERYTHING into one file? Can't I just have it all separated out? Make the file names contextually relevant - so it's no longer just "functions.php" but the file name means something about what's contained "

Well it turns out it's not quite that simple. Wordpress does expect certain things to be present in certain locations. So while I'd love for searchform.php to be somewhere else, it needs to be in the theme's root.

There's some options here:
  • Say "sod it" to the convenience "get_xxx()" type functions.
  • Over ride those convenience functions to look in the relevant folder for 

Anyway, this is how it's all looking at the moment (directory structure):

│├ header.css
│└ search.css
│├ admin-bar.php
│├ header_menu.php

│└ footer_menu.php
│├ favicon.png

│├ icon_search.png
│└ logo.png
├ footer.php

├ functions.php
├ header.php
├ head.php
├ index.php
├ menu-header.php
├ searchform.php
├ sidebar.php
└ style.css

Depending on how much this annoyst me, I may move things like footer.php, header.php, searchform.php etc. into their own folder - something like "elements". This would mean that the theme root could be concerned with the absolute essentials (functions.php, style.css, index.php as well as a few files that aren't there yet like single.php and comments.php). This could make things feel a little more modular. I could take the header, both the php and css file, and use it in a completely different theme without having to rewrite all that much...
Oh - and I made a few mistakes which I've since found in my last couple of posts... but we'll visit those in subsequent posts.

No comments:

Post a Comment