Where do I put the code snippets I found here or somewhere else on the web?

Many posts here or somewhere else contain code, but they do not say where to put it.


I have found this post: How do I turn off 301 redirecting posts (not canonical)?
I’m a newbie with PHP. Where exactly should I place the code from the answer?


Whenever you find a piece of code without clear installation instructions it is probably a plugin. The example you gave is a good one, because that is the most common case:

add_action('template_redirect', 'remove_404_redirect', 1);
function remove_404_redirect() {
// do something

To use such a snippet, put it into a plugin:

  1. Create a new file, name it for example remove_404_redirect.php.
  2. Write simple plugin headers into the file at the very beginning. Use the URL where you found the code as Plugin URL and the code author as Plugin Author:

     * Plugin Name: Remove 404 redirect
     * Description: Disable redirects to similar posts.
     * Plugin URI:  https://wordpress.stackexchange.com/questions/44740/how-do-i-turn-off-301-redirecting-posts-not-canonical
     * Author:      William
     * Author URI:  https://wordpress.stackexchange.com/users/9942/william
  3. Put the code you want to use below the plugin headers.

  4. Install the new plugin.

That’s All Folks.

You could add the code to your theme’s functions.php. But that is not a good idea:

  • Usually, the code is not intended to change the visual representation of your site’s data. But that is the only purpose of a theme. Do not mix responsibilities.
  • Code in the functions.php cannot be turned off separately. If the code breaks one day you have to edit the functions.php again, or you have to switch themes. If you want to use another theme, you have to copy & paste all that code again.
  • If you put more and more snippets into the functions.php you get a unmaintainable mess over time.

Related: Where to put my code: plugin or functions.php?

Leave a Comment