One of our clients runs a WordPress website with many contributors. With so many people writing content for their website, it’s very important for posts to conform to guidelines for the sake of consistency.

One of these guidelines is that the featured image of the “post” (default blog) post type be set. We set out to implement a feature that allows users to publish their post only if they set the featured image.

This feature needed to meet these requirements:

  • Always save the post
  • Display a notification to the user that their post has been updated
  • Display a notification to the user that their post has not been published
  • After adding a featured image, publish the post with no extra steps

This featured proved to be slightly tricky. As I understand it (the author), Wordpress follows this process when saving a post.

  1. User creates new post
  2. User adds content to post
  3. User hits publish
  4. Wordpress POSTS data
  5. Wordpress completes POST request, then does a GET request to display any notifications to the user

My original idea was to just catch a POST request and conditionally react to it. But, this proved to be an issue.
The problem was that, I had to wait for the completion of the GET request before I could know if the user provided a featured image. If they didn’t, I would fire off a second GET request to change the post to draft. This was a bad experience for both the author user and the guest user. The author user would see too many redirects. And the guest user might see posts quickly be created and then removed on the frontend.

So, I decided I needed to know if the featured image was added before the POST request was sent. Then, if the user didn’t add a featured image, change the post status to draft, then allow the post to be saved to the database.

Afterwards, when the GET request happens, I need to add a notification to the user. I also need to remove the post updated notification, because that could be confusing to the user.

Here’s how I did it

Took a second to figure out but then I got it to work!

Next Article

mylistings: looking for beta testers—we want you!

A new version of mylistings is up and running! We’ve made a whole …

View Article