A few weeks ago, Darren from ProBlogger asked me to create a new WordPress plugin for him, one which would save him countless hours that he had to spend before. That plugin is the Group Writing Project plugin, which is now used to help simplify the process that Darren has to go through whenever he decides to start a new project.
I’ve posted about the non-technical side of the plugin and the benefits that he gained from the plugin before, so in this post, I will walk through how a plugin like this is created, and the typical workflow and thinking that goes behind creating something like this.
This is a WordPress plugin, but it uses some special WordPress functions that I have never seen used in an existing plugin before. Hopefully this post will inspire WordPress plugin developers to use these functions more in future plugins.
The most important thing about the plugin is the wp_insert_post() function that I use. This is used to create new posts in the blog, just by using code and by passing an array to it, containing the data necessary.
The plugin basically revolves around this one function, because when a new item is submitted with the GWP (Group Writing Project) form on ProBlogger, it actually modifies an existing post. It determines which post to modify based on the post’s title, which is currently Group writing project – LATEST.
The wp_insert_post() WordPress function is not documented at all, so I’ll explain how it’s used. It helps if you already know how to use general functions in PHP.
When using wp_insert_post(), you need to pass an array as the first and only argument to it, like so:
$post is an array that must contain at least the following keys:
- post_author: the user ID of the blogger who is the post’s author; I set this to Darren’s ID
- post_title: the title of the post. If this is not set, I believe the post’s title is set to the ID number of the post
- comment_status: set this to either ‘open’ or ‘closed’; it will most likely be ‘open’
- post_content: the body of the post. This can be blank.
- ID: this is optional, but if it is set to an existing post’s ID, then you will be modifying an existing post instead of creating a new one.
That’s all you need to get it working. I hope the above has helped you out with your WordPress plugin endeavors!