Toggle navigation

Plain - Design and Development in Scranton, PAPlain - Design and Development in Scranton, PAPlain Text - The blog of Plain

Adding is_published() and is_draft() to WordPress

6 January 2016
Colin
inactive
active

Recently we found ourselves needing two simple WordPress functions that we thought might already exist but seemingly do not. A quick look through WordPress’ Conditional Tag list, a Google search or two, and a GitHub source search turned up no such functions. So we made them ourselves.

is_published() and is_draft() do exactly what you’d think they’d do; determine whether or not the current post or page is published or is a draft, respectively.

Obviously they do not need to be used together and, in fact, either one would be enough to use in nearly every use case but we thought having both would make more sense contextually.

For example, if you wanted to load a piece of UI only when a post has been published is_published() makes more sense contextually (even though !is_draft() would work too). Conversely, if you wanted to show the author something in preview mode on a draft is_draft() seems well suited to make sense when reading the code more than !is_published(). Overkill? Yes. That’s Plain folks.

Neither function accepts any arguments. We only needed to determine the status of the currently loaded post or page. Extending the function to accept a $post Object would make sense too.

If you want them I’ve created a gist with the source code for your edification. They are as simple as possible. However, if you have any suggestions on how they could be written better please do share them with us.


Would you like to receive updates from Plain? Subscribe to our mailing list: