There are two places you could put this code: in the theme or in a plugin.
If you want to put it in the theme, you can edit the functions.php
file in your active theme’s folder and paste the code in there.
In your case, I would put it in a plugin, because if you change themes, you’d most likely want to retain that functionality.
When it comes to plugins, you have two options there as well: regular plugin or must-use plugin. The biggest difference here is that a regular plugin must be activated, but a must-use plugin will be active from the moment it was added.
Here is how you can create a must-use plugin:
1. In your wp-content
folder, create a new folder called mu-plugins
.
2. Create a new file called, for example, posts-landing-page-redirect.php
3. Copy and paste the code above
Now, if you go to your Plugins page in WordPress, you’ll see a new filter called Must-Use
, which is where you can see that this plugin is active.
Give it a try and let us know if it works!