This has been a topic that has been on my mind for a while now and I feel the principle of widgetizing, and more specifically that of code separation, to be a vital cog in the future of WordPress as a cms. Before I get into the hows and whys of this topic I would like to explain a bit of my history as a developer, mainly so that you might understand where I am coming from.
Lets hold up right there!
I can imagine that most people have varying ideas of what a model-view-controller is as a design pattern, as well as the varying levels of depth that they are prepared to go in defining mvc on a philosophical level.
Back to my journey!
I then found out about server side processing in the form of php and mySQL databases. And picked up that I could separate my websites into more than one file. e.g header.php, footer.php, widgets.php or whatever I wanted. I could now put my database connection code and functions in one file and my content in another – I was in php heaven.
I soon found my way to WordPress, which greatly demoralized me, as I realized all this hard work I had put into my php file structure was in vein as this new content management system automated the whole process. Anyone could build a whole web application without even learning a line of code. I then began to start on a deep dark road called joomla, which although was painful, was beneficial in the long run and here is why:
Before you stop reading because you are religious about WordPress and feel I have committed sacrilege by mentioning the word joomla I urge you to continue reading as I do have a point. And yes WordPress does win the war at the end of the story. Joomla, by nature, is a fantastic cms. Its coding correctness when used properly – in theory – beats WordPress hands down. Joomla attempts to follow (or rather submit) to popular native programing tactics such as having an extremely thick class based structure and a solid mvc underlying tone.The power of joomla lies in something called a component.
Although powerful, components are extremely fussy, intricately structured and tightly involved with the core of joomla itself. So much so that most joomla developers use component builders rather than stub out the code themselves. The advantage of such an involved system is this: that when a joomla component works – it works damn well. Unfortunately though when it doesn’t work as planned – it has the real potential to break everything.
Having a core model view and controller class is great and makes you feel safe and like a hardcore programmer. Much like our real developer cousins symphony , codeigniter and cake. The wonderful woodland creatures known as joomla developers have done a great job of separating the code into what is theoretically correct. They have ironically shot themselves in the foot though, as there is no obvious code separation between components and core. Which means everything needs to be written from scratch before each new project, testing becomes a pain because you can’t just take out the pieces you want and test them individually and, furthermore, all collaborators either need to balance the entire site development on one joomla installation or somehow work on separate versions – which inevitably leads them to try and salvage whatever remains when they finally install everything onto a finalfinalxxxusethisoneversion2 joomla installation.
I hope I have made my point clear now – that you have to separate your code into parts. Whether you misleadingly call it a component or plugin. It needs to be done for the sake of my and your sanity.
This brings me back to WordPress
I have recently seen the mvc pattern come under unrepairable fire in the WordPress community where the main argument is basically this – don’t try force WordPress to be mvc because it’s event driven. This triggers alarm bells for me, because chucking something out that is as valuable as separating content and logic and anything else resembling the tried and tested design pattern just for an excuse like that is really not good enough. And yes you might say that separating content and logic is too vague to qualify it as mvc, and that mvc is more complicated than merely just code separation but I have two responses to that.
- Mvc is not a design pattern set in stone that has fixed laws of nature (did you know that in codeigniter the model part is not even required?). If you take this view you clearly don’t understand it’s flexibility. As mvc is awesome and can totally apply to WordPress. In fact it already does, look at the database, the front end and back end, is that not already basic mvc at its core?
- We don’t have to swear by the sacred gods of mvc. We can always take what we like and throw out what we don’t. I completely understand the fact that WordPress is event driven and not strictly mvc in its class structure, but this does not mean we must just dismiss it.
I am going to publish an article in the next few days as to how I think we should go about dealing with WordPress in this manner with some practical examples. Feel free to comment and disagree as I am not claiming the holy grail of web development but rather for us to be open minded in our approach to design patterns in WordPress.