Here at Sitepoint our staff is lucky to be able to take advantage of what we call ‘Triple Time’. Three days a month are dedicated to working on personal web projects. This encourages people to expand on their skill sets and allows team members to put their heads together to create new and exciting web-related projects.
Michael Sauter used his Triple Time to develop this WordPress Plugin, tune in below to see what it’s all about! His plugin is available here.
Jess: I’m here today with Michael Sauter, who is a web developer at SitePoint. He’s going to tell us a little about a WordPress plugin that he has created called Orchestra. What can you tell us about Orchestra?
Michael: Orchestra is a WordPress plugin that allows you to write other WordPress plugins. So if you have anything complex to do in a WordPress plugin in the admin area – if you have lots of forms, you need to do lots of things, not just have a setting or two, then WordPress actually provides you with very little help in how to write it and how to structure your plugin.
So what I did is, I gathered components from other frameworks that make it a lot easier to write more complex codes. So, for example, dealing with databases, working with forms. So I’ve wired these components together, put them in a WordPress plugin. Once that plugin is installed, you can write your own plugins on top of this plugin and use the structure and all the help it provides.
Jess: So how did you come up with the idea for Orchestra then?
Michael: So at Sitepoint, I needed to write a WordPress plugin that was really complex. So you couldn’t just add a setting. You needed to have a complex interface with lots of forms that are dynamic, and you needed to interact with the database. And I didn’t want to do all the sanitisation and everything on my own. So I thought there are other frameworks that are really good, and that make it really easy to do that, and that are also a lot easier to maintain. So I just decided to wire these components together and then write my plugin that I needed to write on top of that.
Jess: Who can use Orchestra then?
Michael: Basically, everyone can use Orchestra who needs to write a WordPress plugin. But it is built on modern PHP libraries that require PHP 5.3. So for example, it uses Symfony2 components, Doctrine components. So your web server needs PHP 5.3. Also, if you haven’t dealt with namespaces and lots of new features in PHP 5.3, it will be a very steep learning curve.
Jess: How do you go about using it then?
Michael: So Orchestra comes with a command, that you write the command line that lets you create a new plugin. Basically, it sets up the structure and includes a demo. So you create it, and then you go on just changing it to what you need, adding new entities, adding new controllers, adding new views.
Jess: Can you show us how to do that? Can you put together a screencast, maybe? Show everyone how it works.
Michael: Yeah, sure.
Jess: Cool. Let’s have a look at that.
Michael: I have already installed WordPress, and I have put the Orchestra plugin inside wp-content/plugins folder. Now I’m going to show how you can use it to create your own plugin. So in the terminal, we go to the plugins folder. And then type in “orchestra/console” and there is one command to create plugins. So plugin key and then your name. We’ll call this “demo”.
So this has created a demo plugin in the folder, “demo” You can see it’s up here. What we now need to do is to activate the plugin in the plugin setting in WordPress. So you need to refresh that, and the demo shows up here… and we activate the plugin. You can see there’s already a page in here. But you also need to tell this to update the database. And I’ll show you what this will do. So in demo, there is a bit of code already in there. We model what will be in the database in “Entity”, and I created a person in there. So this will create a table which represents “wp_demo_persons.” And a person has an ID and a name. So when we now tell Doctrine, the object relational manager, to update the schema. We’ll do that. I will just paste that in and update it. So this schema is updated. If we look into our database, we should see a new table here. It’s there. It has an ID and a name.
There’s nothing yet in there, but we can now use the demo plugin to edit, to add new persons to edit existing persons. So as the demo plugin is structured around the MVC pattern we can see here in the default controller, we have index action. So this will get all the persons that are in the database. It will then pass those persons to the default template. Let’s see how it looks. So we go to demo. This, will call the default controller and the index action. There is no person there yet. We then go to “create new person”. And then we will see a form where we can enter “John Doe,” for example. Click “create” and we have created a new entry in here. In the database that’s just ID 1 with name “John Doe”. The form is set up in the “create action”, which is called, so we create a new empty person object, we create a form for it. If we use the POST method to this “create action”, we find the new values in the form, check if it’s valid. If it is, it goes right to the database, and let’s us know that it has happened in the flash, and redirect to the index.
So that’s what you see here. “Person created,” redirect to index. Similar thing for edit. I’ll show you how the template works. For edit, same thing. You create a form. Set the model for it, and then you render the HTML, you pass it the form and the ID. So the view is Default/edit.html.twig. which looks like that. So we have a form action, it points to the default/edit action. And we render that form here… Edit it and say “John Doe 2”. That will edit the person. And we can also delete it.
Basically, that’s the structure of a new plugin. So you have controllers, entity, the forms. And you can change it to whatever you like and use the console to update your database.
Jess: Cool. So that’s how you use Orchestra. If you want to learn more, where can people check things out?
Michael: Basically, as Orchestra is a set of components that are already available everywhere and also very well documented, like you can in Orchestra and the documentation for these parts. Basically, you can consult the Symfony2 documentation, which is very well written. There’s lots of documentation about Doctrine as well, and same goes for Twig. Basically, everything in Orchestra is covered online, so you can read the documentation. The only part that is custom in Orchestra that I wrote is the controller and router. That is modeled around Symfony2 controller. So for the most part, you can just use that documentation, and you’re good to go.
Jess: Too easy. Thanks so much, Michael. This has been Jess Genevieve Brown with Michael Sauter for SitePoint.com
Michael’s plugin is available here.
Visual Studio Code: End-to-End Editing and Debugging Tools for Web Developers
Form Design Patterns
Jump Start Git, 2nd Edition