Skip to content

Adding 'save as draft' to creating posts

When I built the backend functionality of this website I built the ability to create posts, but didn't add the ability to save a post as a draft. I honestly didn't think that would be necessary. I was wrong! I also…

Comment
Adding 'save as draft' to creating posts

From the archive: this post is about the code behind the first steveshead.io, written in 2021-22. The site runs on Xavier now, so the code here is history rather than how things work today.

When I built the backend functionality of this website I built the ability to create posts, but didn't add the ability to save a post as a draft. I honestly didn't think that would be necessary. I was wrong! I also thought it would be a difficult task - I was wrong again. With a little planning I built the functionality into my test site and it worked, and have since rolled it out here.

I created a variable called '$status', and created a database row for two states, 'published' or 'draft'. I added a button for 'save as draft' and checked upon submit which button was clicked. The 'insert' statement would either insert 'published' or 'draft'. I changed the statement to pull posts into the posts page with the 'WHERE' statement 'WHERE status = ?'. Simplistic but effective.

What I have not yet done is add the ability to save an unpublished post that is being edited as draft. The irony is I didn't work out that I had missed that until I started writing this post. Quite ironic, right? I also need to create the ability for a logged in user to see any draft posts they have created.

Why is any of this a thing? Because it's another step in my journey of learning coding. This app is the first one that I have built and used live, and its functionality is growing as I learn. There are still some teething problems - spurious errors in the error log that I am trying to decipher - but - it works - I've even made some sales - that's a bonus.

Anyway - that's enough for now. As always let me know your thoughts, ideas and opinions - always open to feedback.

Happy coding!

Cheers

Steve

Comments

No comments yet - start the conversation.

Code

Adding draft posts: it's a story!

This past weekend I added the functionality to view draft posts. I know that should have been a thing earlier on but I was thinking about it wrong, and unless it's clear in my mind I can't continue. Let me explain. I…