Ever wanted to generate custom URLs in WordPress for your posts? Turns out it’s actually pretty simple.
In this post, I’m going to show you how to generate custom post URLs in WordPress with a few examples. I originally came across this while working on an invoicing system. Every time an invoice was modified a pdf would automatically be generated using the URL as the file name. Obviously, each pdf needed a custom file name. The solution was to generate a new URL on every update.
This is also useful for news site running updates on stories they previously covered with the same URL.
To keep it basic we’ll start with just adding time() to the URL.
Now before you speed off let’s just make sure we double check a few things. First, check if the post has a title and if the post is actually published.
Next, make sure this function only runs when you publish a post and not every time you update a post. This if statement simply checks if the post date matches the last modified date. If the last modified date is past the post date the URL will not change.
So how else can this be used?
Here are a few examples. Simply switch out the time() function with any alternative.
Of course, you can add almost anything to your custom URLs. Hopefully, this post has helped you.