I’ve always believed that good developers automate any task they have to do more than once. And you know what? If you try to automate as many of your tasks as possible, you might get lucky and never work again some day 😜 (which is a quite convincing argument, isn’t it? 😇). I’m already on my way to early retirement and, for example, I’ve recently created a tiny script that simplifies the process of releasing new versions of our plugins.
As a WordPress plugin developer, your task should also be to make the life of your users easier. You can achieve this, of course, by creating a plugin that literally automates their tasks (such as we do with Nelio Content, which fully automates the promotion of their WordPress blog on social networks). But making lifes easier is not only about automations; you can also save your users work if you simplify their tasks, reducing, for example, the number of steps needed to complete them. And this is exactly what I came to talk to you about today.
A couple of years ago I wrote a post on how to create your first plugin in WordPress. I explained to you, step by step, how we could put a box on the edit post page to add extra information to your post. Well, today we’ll see what we can do to simplify life for our users and improve their performance when using our plugins.
Let’s Get Started—Simple Plugin with One Meta Box
Imagine we have a plugin like Nelio Content that reshares your blog’s content in social media. The users may be interested in certain posts not being automatically reshared, so they need some mechanism to tell the plugin what can or can’t be shared. For the sake of simplicity, we’ll assume this can be done by using a checkbox in a custom meta box:
If the user checks the box, the post won’t be automatically reshared. If it’s unchecked, then our plugin will be able to reshare it whenever it feels like it. Easy peasy, right?
Users Should Drive All Your Decisions
If we’re to make our users’ lifes easier, the first thing we have to do is identify the tasks that need simplifying—that is, those that are the most labor-intensive. In my example, such a task is clearly the process of configuring which posts can or can’t be reshared. Let’s take a closer look at this.
Identify the Problem
The user’s goal is to easily define which posts should or should not be reshared by the plugin. Unfortunately, the current version of our mock plugin requires the following steps for changing the reshare option of a single post:
- Find the post in the All Post list
- Click on the Edit link
- Wait until the Edit Post screen is loaded (which might take a few seconds)
- Scroll down and look for the plugin’s meta box
- Click on the checkbox to toggle its value
- Scroll up and look for the Publish box
- Click on Save
Boring, right? 😩 Then remember this: your users have to do that every time they want to change a single post! Damn 😭
Once we know the problem we want to tackle, it’s time to think about possible solutions. For example, from the top of my head:
- Use the fact that posts are organized into tags and categories and use them to define what should be shared. For instance, with the proper UI, we’d be able to tell our plugin: “share all posts in WordPress, and don’t share any in Business“.
- Using Bulk Actions in WordPress, we can add the checkbox directly in the All Posts list and modify the reshare option in more than one post at a time.
When considering multiple solutions, remember that they all have some pros and cons. You’ll have to choose the best option keeping your users in mind. In this particular case, I’d probably choose the second option: the checkbox is something our users already know (they’re used to seeing it in the meta box) and they perfectly understand how it works. Besides, adding it in the Bulk Edit is a straight-forward process. Conversely, the first solution I thought of is way more complex—sure, it’d be also more flexible, but it’d also be more difficult to implement and more complicated to use.
How to Add New Bulk Actions in the All Posts List
Adding new Bulk Actions in WordPress is actually pretty simple. Just follow these four easy steps:
- Add a new column in the Posts list. This might seem unnecessary, but we have to because Bulk Actions can only modify attributes that are in the list.
- Add the proper values in the new column.
- Add the checkbox in the Bulk Edit form.
- Tweak WordPress to make sure that the value is properly saved when the user submits the Bulk Edit form.
If you do it properly, you’ll have this:
Adding a New Column in the All Posts List
This step is very simple—just hook into the
manage_posts_columns action and, for each post type you’re interested in, add the new column:
Set the Proper Values in the New Column
In this case, we’ll use an action named
manage_posts_custom_column. The callback function receives two params, the column for which we’re setting the value and the related post ID. The code is very simple too: we simply need to make sure we’re setting the value of the column we just created (lines 6 to 8) and, if everything’s OK, output the specific value the current post has (lines 10 to 14):
Adding the Checkbox in the Bulk Edit Form
Once again, we use WordPress’ filters and actions to get the job done. This time, we hook into
bulk_edit_custom_box and simply output the form field we want:
Now, did you notice I didn’t print a checkbox, but a
select? You might be wondering why I did that… and the answer is pretty simple: checkboxes have a binary state (either they’re checked or they aren’t). In bulk actions, however, you usually want a third state, which is “don’t change anything, leave the values as they are”. So, in order to do that, I have to print a field with three values—a
Save the Value (Probably)
Last, but not least, we have to make sure that whatever the user selected value is, it is saved into the database. This can be easily achieved by using the
save_post action. The callback function simply need to retrieve the user’s input from the
$_REQUEST array and, depending on its value, update the database:
Today we’ve seen how easy it is to add Bulk Actions in WordPress, and how helpful they are! But beware, don’t be tempted to use them all the time—we decided to use this solution after we identified a problem and carefully decided it was the best solution possible. You should also follow this reasoning when facing a similar issue.