Hello Computer 🙂
The WordPress ecosystem works with an interaction between three components. The WordPress core, themes which change a website’s appearance and plugins which modify functionality.
At WordCamp Cape Town I attended a workshop presented by Jonathan Bossenger on writing your first WordPress plugin, and I set myself the deadline of 30 September to write my first plugin “The Turtle Moves” which is going to populate the end of posts with a random quote from Sir Terry Pratchett from a pre-defined quote collection. (Upon reflection I decided that the end of posts meant the very end after the loop has finished, and after the comment section.)
A WordPress plugin needs only one PHP file with a specifically formatted DocBlock. The Header requirements are handily listed in the WordPress Developer Plugin Handbook. The only requirement is to have the plugin name.
So, I started off by writing the header comment, in a file that I named the-turtle-moves.php I saved it in a local website in the plugin directory. As a plugin is a collection of PHP scripts, I opened my file with a <?php tag.
While my end goal was to have the plugin add a quote at the end of a post I decided that for the purpose of testing the initial functionality I was going to mimic Hello Dolly and have the quote echo on the admin screen. This was a good decision because it allowed me to copy some of Matt’s code directly, and when I got parse errors that prevented me from activating the plugin it kept me on an even keel without going down an anxiety spiral. Both parse errors that I encountered were caused by missing semi-colons, and I am grateful to the helpful contributors to Stack Overflow which pointed me in the right direction to debug.
After resolving those issues the biggest challenge that I had was finding the correct filter to use to insert the quote into the site. A problem that I had a couple of times is by using the filter I ended up removing other WordPress functionality, (e.g. the actual post content or the entire comments section). The moral of the story is that I need to learn more about WordPress action and filter hooks.
Once I figured out that the correct code was an action hook onto “after_loop” it was smooth sailing. I then zipped up my plugin and loaded it onto a different local test site where it worked as intended. I am proud that I got this plugin done. It is nothing spectacular but it was a very good first step into plugin development and getting an understanding of how plugin development works. If you are so inclined you can check out The Turtle Moves on GitHub.
Love and plugins,