Skip to content

Conditionally Redirect in WordPress

Blog  ✺  WordPress  ✺  Code Snippet
Conditionally Redirect in WordPress

For a recent client project, I needed a way to redirect to their "Careers" page in case the job ad is currently not published.

The client has a set of roles that they hire for periodically. Their job ads therefore are published when they have a vacancy and set as draft once the position is filled.

In the WordPress hook system, I decided to use the template_redirect action to check if the queried object is a job ad that is currently set to draft.

That's it, you can adjust the code to your liking. There are other ways to redirect in WordPress, but I decided on template_redirect because this action hook executes just before WordPress determines which template page to load. It is a good hook to use if you need to do a redirect with full knowledge of the content that has been queried.


Before you go, let me know on twitter if you like this type of content? Feel free to sign up to my newsletter for more.

Comments

You might like

Calm: Peaceful Pictures

Opinion  ✺  Photography

Background File Upload in Filament Forms

Filament  ✺  Video