In the meantime, I decided to start a new project that I’ve been meaning to do for a long time: create a WordPress plugin.
Why a Plugin?
There are a few reasons I want to create a plugin.
First, I want to be doing more in WordPress than building and maintaining sites. I can create custom CSS all day and even tweak themes, sure. I know enough PHP to get myself into trouble. I want to do more than read and tweak PHP code — I want to write useful programs.
Second, creating a project like this will push me further along than just following tutorials all day. If I have to figure out how to apply knowledge in a practical way, I think I’ll retain it better. I’ve written PHP when building a movie database site, but that was mostly following a book. On that project I came up on some stumbling blocks where I had to solve problems that the book hadn’t outlined. I feel like those blocks did more for my education than if it had all gone smoothly.
Creating a plugin from scratch will be a big learning curve. I’ll have to do more than follow a tut, and I’m excited for that challenge.
Lastly, I have personal motivation for writing this plugin. Earlier this year, I got my teen son to start writing movie reviews as a homeschooling assignment. Part of the assignment is to spell out his thoughts on what he thinks the movie will be like before watching it. Things like expectations, what he’s heard, trivia, etc..
I want those “Pre Watch” thoughts to appear above the review post. I also want to make it easy for him to write it without jumping through hoops on his end. I want him to focus on the content itself.
Armed with reasons and a use case, I finally got to work.
How it Started
The first thing I did was, of course, look for a tutorial on creating a plugin. I wasn’t looking to follow one, however. I just wanted to get an overview of what goes into creating a plugin. What are the steps? I wanted to see someone do it and understand how to even approach a project like this. After some research, existing plugin dissection, and reading — including lots of references to my Professional WordPress book — I had a plan.
At least, I knew that I would need to create:
- A custom post type
- A custom field
- A custom table in the site’s database
With that very spare plan, I dove in. I was already familiar with creating a basic plugin that will show up when you add it to your site. And it turns out that creating a custom post type was also pretty easy.
Creating a custom field was trickier. There’s a plugin that will just do it for you, and I toyed with the idea of using it. But I wanted to understand the underlying code and how it worked, rather than just make it happen by magic. I found a lot of info about writing one, and I tried a few examples. Making a custom field or meta box appear in the post editor isn’t too difficult. The challenge lies in getting that data to post to the database when you hit Publish.
It worked. And then it didn’t. Then it did, but not the way I wanted. So far, I had a plugin that created a menu in the WordPress Admin Bar, and posted to categories within a custom post type called Movie Reviews.
Not quite there yet.
How it’s Going
I’ve had some successes and I understand a lot more about plugin functionality than I did before. That’s great, but now I have two main problems to solve:
- Get the custom field to create a new table in the database and save data to it
- Display the custom field (Pre Watch Thoughts) in a Movie Review post
One thing that I forgot was that I’ll need to create a new single post template that overrides the WordPress default single.php template. It also needs to work regardless of the theme that’s used, which is getting trickier with WP 6.1. For now, I just want to get it to work with the Twenty Twenty theme and I’ll sort that out later.
I decided to create a schematic of my plugin to have something that will keep me on track. I’m a very visually-oriented thinker, and this helped a lot.
I’m also questioning the need for a new table in the database. Maybe I can just post to the existing post_meta table? I need to do more reading on that.
So this is where I am right now. I’ve put this aside more than once over the past few months, because of work or fear that I just can’t figure it out. But now I’m motivated again, and I’ll be putting in steady work on this over what’s left of the holidays.
I’ve considered asking someone to coach me on this. Not to give me answers, but to guide my thinking. I may still do that. My latest code is in my Github repo, so if you have feedback, I welcome it.
Where am I thinking about this wrong?
What am I doing right?