Sit Back And Relax, Put WordPress Post Management On Auto-Pilot
Sit Back And Relax, Put WordPress Post Management On Auto-Pilot
The only WordPress post task that you can schedule is publishing but wouldn’t it be good if we could also schedule the closing of comments, the expiring of a post, adding and removing a post from featured content, even restricting or unrestricting access?
In this Weekend WordPress Project, we’re going to put get on that path to schedule heaven with a new plugin with seven new actions and a look at how to extend it with our own custom actions.
Being able to schedule actions on posts makes managing and maintaining a site so much easier. We couldn’t run this site without scheduling as we publish to Eastern Standard Time but the majority of the posts are written in a timezone that is either 14 or 16 hours ahead.
There are many other actions that, if scheduled, would make site owner’s lives considerably easier: enter the Schedule Content Actions plugin.
With only 183 downloads, this plugin has obviously slipped under the radar of most WordPress site owners yet not only is it useful in its own right, it’s also dead easy to extend and customize for your own situation.
Once you install the plugin, a metabox is added to the bottom of any post type, builtin and custom, so everything here would apply to events, reviews, even products. (I’ll be using the term post to cover any post type.)
The metabox allows you to select an action and configure a date and time for that action to take place. Now, the only drawback with the plugin is that the scheduling time is UTC, so that might take a little bit of getting used to.
The plugin comes with the following actions:
- Stick / Unstick Content: these actions are useful for controlling the prominence of posts in situations where the post listing outputs sticky posts first (default behavior). Some themes and sliders may also use the “sticky” attribute to select content.
- Draft / Trash Content: these actions change the status of the post and will have the effect of removing a post from a site, or “expiring” it. Useful if you only want a post to be visible for a set amount of time, such as an limited-time offer.
- Delete Content: this action goes the whole hog and actually deletes the content. Not sure I’d advocate using this action when the Trash action is also available.
- Open / Close Comments: these actions, as the names suggest, control whether comments can be posted or not. This can be useful for any post where you want comments to only be open for a fixed timeframe, such as for feedback on a proposal or a competition where the comments are the entry.
All pretty good stuff but what’s intriguing about this plugin (and why it’s such a great candidate for a Weekend WordPress Project) is that is really easy to extend with your own actions.
Adding Your Own Actions
If you’re anything like me, then when you stumble across plugins like this you start to think about how you can extend it to meet some additional scenario.
In that spirit, and just to show you how easy it is, I’m going to walk you through adding four extra actions:
- Feature / Unfeature Content: these new actions will add and remove a “feature” tag to the post allowing you to control their inclusion as featured content.
- Restrict / Unrestrict Content: working in conjunction with the Restrict Content plugin, these actions will either place or remove restrictions that prevent guest visitors from viewing a post.
The Scheduled Content Actions plugin is beautifully put together and easy to follow. To add our new actions, we need only change two files and add two new files with the functions to carry out our new actions.
But before we do that, let’s quickly review how the plugin works.
Plugins, Theme, Action!
When you add a scheduled event to a post, it actually gets stored in the global option _sca_current_actions which contains an array of post_ids, datetimes and actions.
A function is attached to the wp_loaded action and therefore gets triggered when all plugins and the theme have been loaded. The function checks through all the actions and carries out the action (actually by using the do_action function) where the current time is greater than the scheduled time.
1.6 million WordPress Superheroes read and trust our blog. Join them and get daily posts delivered to your inbox - free!
When the action is completed it is removed from the global option.
So, to add our own custom actions, we need to write the action code and add our actions to the action list.
Letting The Plugin Know About Our Actions
The /scheduled-content-actions/scheduled-content-actions.php is basically a list of files to be included and we just need to add two new require_once statements for our new function files, action-feature.php and action-restrict.php, to the content actions list:
You’ll notice that rather than creating four new files, we are only creating two. It seemed a little verbose to create separate functions for each function and so I’ve grouped them into two files, one for the feature functions and one for the restriction functions.
The /scheduled-content-actions/inc/actions.php sets up all the actions and we just need to add our new actions (feature_content, unfeature_content, restrict_content and unrestrict_content) to the list
Now we just need to add our custom action functions.
Creating The Custom Action Functions
We’ll follow the plugins naming convention and create two new files: action-feature.php and action-restrict.php both of which will need to be added to the /scheduled-content-actions/inc/ folder.
This adds two new custom actions, sca_feature_content and sca_unfeature_content. The former simply adds the tag feature to the post using the wp_set_object_terms function; the latter removes the tag using the wp_remove_object_terms function.
Not really much more complicated, this works on the fact that the Restrict Content plugin uses post meta data to track whether a post is restricted or not.
The sca_restrict_content function simply adds the two post meta attributes rcUserLevel and rcFeedHide to let the Restrict Content plugin know the post is now restricted. You’ll notice that the rcUserLevel is given a value of Subscriber: if you want to use a different, or custom, role, simply change this value.
The rcFeedHide attribute tells Restrict Content to exclude the content from any RSS feed. Change this value to off if you don’t want this behavior.
Not surprisingly, the sca_unrestrict_content function removes the restrictions by deleting the post meta attributes.
You could use this functionality to allow subscribers exclusive access to the content for a set period (use it in conjunction with the close_comments action to allow subscribers to get in first with feedback or competition entries). Or conversely, you might have all your content freely available for a period of time (for example seven days) and then restrict its access to subscribers.
Don’t forget to install the Restrict Content plugin if you want to use these actions!
Schedule, Sit Back And Relax
Scheduling these kinds of actions on our content can potentially save us a great deal of time and effort, especially when dealing with reasonably large and / or reasonably busy sites.
The beauty of the Scheduled Content Actions plugin is that it is really easy to extend beyond its builtin list of actions and it doesn’t take much to be able to leverage other plugins’ functionality. All, of course, whilst keeping everything on auto-pilot.
What actions would you like to be able to schedule? List them in the comments below and, who knows, maybe there’ll be a follow-up “how to” post.